Thread: Dll Problem

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    19

    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!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    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);
    Last edited by Davros; 12-01-2002 at 01:06 AM.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    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
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >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.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    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!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Then stick to flat functions using the standard calling convention.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    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);
    }
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    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.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    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!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  10. #10
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    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.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    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!!!!!!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  12. #12
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 10:11 AM
  2. Replies: 1
    Last Post: 09-18-2005, 09:06 PM
  3. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  4. std::string vs char* DLL problem
    By aker_y3k in forum C++ Programming
    Replies: 13
    Last Post: 10-02-2002, 09:05 AM
  5. VCL and DLL class problem
    By borland_man in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 11:07 AM