-
Dll Problem
Hi! I am aking my first DLL and I wrote a function to play a sound and then I wrote a VB app to call on the dll. When I run my App, it gets a run time error saying my DLL has a
"Bad Dll Calling Convention"
Here is my Dll code.
#include "stdafx.h"
#include <mmsystem.h>
extern "C" __declspec(dllexport) void PlayWav(char File);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
void PlayWav(char File){
PlaySound(&File,NULL,SND_FILENAME|SND_ASYNC);
}
What am I doing wrong? Thanks!
-
I'm no expert with VB, but I understand that VB can only use the standard calling convention (__stdcall). While you are exporting your function using the C calling convention.
Try this (I think it's correct):
extern "C" void __declspec(dllexport) __stdcall PlayWav(char File);
-
Ok, I changed my code using a class as so
class PlayWav {
public:
PlayWav();
PlayWav(char File){
PlaySound(&File,NULL,SND_FILENAME|SND_ASYNC);
}
};
However, when my app runs it, I get another runtime error saying
"Can't Find Dll Entry Point PlayWav in DreamLib.Dll"
Argh..... What am I doing wrong? Thanks
-
>Ok, I changed my code using a class as so
Are you saying you now are trying to export C++ classes from a DLL to be used by a VB app? I don't recommend this as it's going to put you on stoney ground, unless you want to get into COM.
-
I don't wanna make a com file. I just wanna write a Dll function that can be executed my other programs calling on it. That's all I want. Any ideas? Thanks!
-
Then stick to flat functions using the standard calling convention.
-
So what do I need to change? What am I doing wrong?
// DreamLib.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <mmsystem.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
PlayWav(char File){
PlaySound(&File,NULL,SND_FILENAME|SND_ASYNC);
}
-
Include the prototype:
extern "C" void __declspec(dllexport) __stdcall PlayWav(char File);
and change your PlayWav definition to
Code:
void __stdcall PlayWav(char File)
{
...
}
There is stacks of info and examples on the web, if you search for it.
Finally, I assume you're using VC++ as your compiler. If not, you may run into name decoration problems with standard call.
-
Yes, I am using VC++.NET as my compiler and I just tried what you told me but I still got the same
"Can't Find Entry Point" runtime error. Any other ideas? Thanks so much!
-
Try defining your DllMain as:
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
notice the WINAPI, rather APIENTRY
Beyond that, don't hink I can help. Find an example where someone has built a DLL in C/C++ for use with VB.
Edit : In fact, I think WINAPI is nothing more than a #define for __stdcall.
-
Hmmm... yeah that still doesn't work. Thanks though.
Wasn't WinMM.Dll written in C++... or Assembly? Or maybe both. Either way WinMM.Dll works with Vb. Thanks so much!!!!!!
-