none
dlls and calling convention

    Question

  • Hi all,

    I have this problem with a project I am working on and I thought maybe asking a question here might give me some insight and possibly lead me to a solution.

    I have a DLL that is written in C/C++ and it is required to assume __cdecl calling convention (MUST).  I also wrote a wrapper DLL that calls into this dll with __stdcall calling convention.  The reason I have this wrapper is because I have to use a VB6 application and based on suggestions from MS, this is the proper way to structure the code (http://support.microsoft.com/default.aspx/kb/153586?p=1)

    I followed all the instructions but when I run the VB6 application from the IDE, it crashes with the Send/Do Not Send Error Report dialog box.  When I comment the call from the wrapper dll to the main dll, the crash goes away. 

    Are there any project properties in VS2005 that I need to set in order for this to work without crashing? 

    Can anyone explain to me a little bit about how does the OS handle a DLL with a __stdcall calling convention calls a DLL with a __cdecl calling convention?

    I am aware of and understand the difference between the two calling conventions but not sure what happens when they are mixed in a project like mine.

     

    Thanks.

    Sunday, April 04, 2010 10:30 PM

Answers

  • To be honest, if you followed the guide then there shouldn't be any problems. But the application crashing doesn't necessarily have to be because of calling convention issued. Have you wrote a sample C++ program to call the stdcall DLL to see if it works? You will also get the chance to debug it properly this way too.

    *Edit*

    As an example for you to test out.

    //testdll.cpp
    extern"C"
    __declspec(dllexport) int testfun()
    {
    	return 3;
    }
    
    //testwrap.cpp
    extern"C"
    __declspec(dllimport) int testfun();
    
    extern"C"
    __declspec(dllexport) int __stdcall testwrap()
    {
    	return testfun();
    }
    
    //main.cpp
    #include <tchar.h>
    #include <stdio.h>
    
    extern"C"
    __declspec(dllimport) int __stdcall testwrap();
    
    int _tmain()
    {
    	int num = testwrap();
    
    	_tprintf(_T("%d\n"), num);
    
    	return 0;
    }
    If you compile the first one into one dll, the second into a second dll linked with the first and then the third into an executable linked with the wrapper, then try it out it will work with no problems. Of course, I left all of the __declspecs inline to make things clearer and to keep things small.


    Visit my (not very good) blog at http://c2kblog.blogspot.com/

    • Edited by Crescens2k Sunday, April 04, 2010 11:11 PM Added sample code
    • Marked as answer by Nancy Shao Tuesday, April 13, 2010 5:40 AM
    Sunday, April 04, 2010 10:46 PM