www.digitalmars.com         C & C++   DMDScript  

c++.windows.32-bits - Create DLL callable from VB/VBA?

reply E Chandler <E_member pathlink.com> writes:
I'm trying to create a DLL which I can call from VB/VBA (Office 97). The DLL
appears to have a valid entry point and the calling conventions seem correct but
VBA gives me an error 48 - "DLL not found" which sometimes means the DLL is not
properly constructed even though it is there.

my command line is:

dmc test.c -WD -S -L/Entry:DllEntry

test.c is:

long __stdcall DllEntry(void *i,void *j,void *k){
return 1;
}

long __export __stdcall isub2(long *i,long *j){
return *i-*j;
}

test.def is:

LIBRARY "test.dll"
EXETYPE NT
SUBSYSTEM WINDOWS
EXPORTS isub2=_isub2 8

I have been able to create DLLs which work using Masm32 and various other C
compilers but so far I am missing something needed to get this to work with
Digital Mars.
Apr 18 2005
parent starkweatherr mchsi.com writes:
I need a DLL that I can call from VB, so I would be interested if anyone comes
up with a fix for your problem. The only DLL example I have found uses __cdecl,
which VB doesn't like unless you use some macros. Example code:

// dmcdll.c
#include <windows.h>
#include "dmcdll.h"

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:  // Colon not semi-Colon
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
{
__declspec( dllexport) int DoSomething( int a, int b)
{
return a*b*2;
}
}
-> Build DLL command: dmc -mn -WD dmcdll.c kernel32.lib

// dmcdll.h
// If I am not mistaken, it is not necessary to include the variable
// names here, only the types, but this is the way the sample was done.
__declspec( dllexport ) BOOL APIENTRY DllMain(HANDLE hModule, DWORD
ul_reason_for_call, LPVOID lpReserved) 

__declspec( dllexport ) int DoSomething( int a, int b);

-> Build lib with: implib /noi /system dmcdll.lib dmcdll.dll

// dmctest.dll
#include <stdio.h>
#include <windows.h>
#include "dmcdll.h"

int main()
{
printf("Result: %d\n", DoSomething( 12, 32);
}

-> Build exe with dmc dmctest.c dmcdll.lib

TestDLL.exe works as advertised but VB returns an Error 49 "Bad DLL calling
convention."

What's the rest of the story for using __stdcall?
Mar 17 2006