Solved

C++ DLL with Dev-CPP for use in Visual Basic 6

Posted on 2003-11-23
14
18,421 Views
Last Modified: 2007-12-19
My Dll's export are twined and decorated
ex: For the function void __stdcall HelloWorld(void) i've got : _Z10HelloWorldv@0 and _Z10HelloWorldv

How do i get a export that i can just call HelloWorld without Alias "_Z10HelloWorldv" ?

Here's my Source :

==========================
Def.h
==========================

#include "windows.h"
#define export __declspec (dllexport)

export void __stdcall HelloWorld (void);

==========================
main.cpp
==========================
#include "Def.h"

void __stdcall HelloWorld (void)
{
    MessageBox(NULL,"Hello !","Message",MB_OK);
}
0
Comment
Question by:ennixo
14 Comments
 
LVL 12

Expert Comment

by:vascov
Comment Utility
extern "C"
{
      export void __stdcall HelloWorld();
}


extern "C"
{
      void __stdcall HelloWorld()
      {
            // something
      }
}



HTH
0
 
LVL 5

Author Comment

by:ennixo
Comment Utility
thanx a lot ! no more decoration !

but my exports are still twins :
HelloWorld
HelloWorld@0

can't i get only HelloWorld alone ?
0
 
LVL 12

Expert Comment

by:vascov
Comment Utility
extern "C"
{
  export void __cdecl HelloWorld();
}

extern "C"
{
  void __cdecl HelloWorld()
  {
    // something
  }
}


Either that, or use the /EXPORT and .def file approach.

HTH
0
 
LVL 5

Author Comment

by:ennixo
Comment Utility
well i tried with __cdecl, it works but then the function cannont be called from VB6...

and i tried with a .def file but i think i don't write the good code because nothing changes :
here is what i tried in testlib.def
LIBRARY testlib
EXPORTS
    HelloWorld    @1


i tried a few things in the .def file but no result...
what should i write ?
0
 
LVL 12

Expert Comment

by:vascov
Comment Utility
If you use __stdcall or __cdecl, VB will know how to find it's way into the correct entrypoint.
Did you get any error when calling from VB6 ? (i tried it with both conventions, and both worked)

On any case, if you really need the entrypoint undecorated:

cl mylib.cpp /EXPORT:HelloWorld

or

cl mylib.cpp /LINK /DEF:mylib.def

where mylib.def is:
LIBRARY mylib
EXPORTS
       HelloWorld      @1


HTH
0
 
LVL 5

Author Comment

by:ennixo
Comment Utility
sorry i thought i said i am a newnewnewbie to C++ and its compilation...

cl mylib.cpp /LINK /DEF:mylib.def
where do i put this ???

__stdcall works perfectly, my vb calls HelloWorld and it works but there are still 2 exports
__cdecl there is only "HelloWorld" export but my VB didn't find the entry point...
0
 
LVL 12

Expert Comment

by:vascov
Comment Utility
Sorry :)

Which version of VC++ are you using ?
Can you also share the Declares you're using in VB6 ?

Assuming VC7.1 (2003), you should:
Menu Project
Properties
Linker
Input
Module Definition File -> set this to the .def file you've just added into your project

Let me know if this works for you.

HTH
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Author Comment

by:ennixo
Comment Utility
i don't have VC++, only Dev-C++ (http://www.bloodsheld.com) :-/

but i found --no-export-all-symbols --add-stdcall-alias in the linker (Project > Properties > Linker)
i tried to add myfile.def, it changed a little the compilation but nothing changed in the dll...
0
 
LVL 9

Expert Comment

by:Dang123
Comment Utility
Listening
0
 
LVL 12

Expert Comment

by:vascov
Comment Utility
Hi, i'll have to download the compiler and play with it.

(downloading as we speak)
0
 
LVL 12

Accepted Solution

by:
vascov earned 125 total points
Comment Utility
Downloaded Dev-Cpp / MinGW and built a standard DllProject.
Worked perfectly with __stdcall and __cdecl :)

Can you send me your project configuration ?


Here's the code i developed:
// BEGIN dll.h
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


extern "C" __declspec( dllexport ) int HelloWorld();
extern "C" __declspec( dllexport ) __stdcall int HelloStdCall();
extern "C" __declspec( dllexport ) __cdecl int HelloCDecl();


#endif /* _DLL_H_ */
// END dll.h

//BEGIN dllmain.cpp
#include "dll.h"
#include <windows.h>

extern "C" __declspec( dllexport ) int HelloWorld()
{
    return 1;
};

extern "C" __declspec( dllexport ) __stdcall int HelloStdCall()
{
    return 2;
};

extern "C" __declspec( dllexport ) __cdecl int HelloCDecl()
{
    return 3;
};


BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    return TRUE;
}
// END dllmain.cpp


Compiled it with:
g++.exe -c dllmain.cpp -o dllmain.o -I"C:/Dev-Cpp/include/c++"  -I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  
dllwrap.exe --output-def libProject1.def --driver-name c++ --implib libProject1.a dllmain.o  -L"C:/Dev-Cpp/lib" --no-export-all-symbols --add-stdcall-alias  -o Project1.dll


Now, running dumpbin.exe /EXPORTS (needs Platform SDK or VS or Windbg package)
dumpbin /exports project1.dll

    ordinal hint RVA      name

          4    0 00001184 HelloCDecl
          1    1 0000117A HelloStdCall
          2    2 0000117A HelloStdCall@0
          3    3 00001170 HelloWorld


Then i made a very small test program in VB6,
Module.bas
Public Declare Function HelloWorld Lib "project1.dll" () As Long
Public Declare Function HelloStdCall Lib "project1.dll" () As Long
Public Declare Function HelloCDecl Lib "project1.dll" () As Long

And created a button in a form with the following code:
Private Sub Command1_Click()
    MsgBox HelloWorld
    MsgBox HelloStdCall
    MsgBox HelloCDecl
End Sub

This resulted in three msgboxes poping up, with 1, 2 and 3.

One thing you have to make sure is that it finds the most up to date and correct DLL.






0
 
LVL 5

Author Comment

by:ennixo
Comment Utility
ok, thanx a lot !!!!
i learned that i must have HelloWorld and HelloWorld@0 because of stdCall
and stdCall is absolutely needed for my functions to be called by VB... (CDecl really doesn't work with VB)

i think you can ear more points from me with this question :
http://www.experts-exchange.com/Programming/Programming_Languages/Q_20806513.html
0
 
LVL 5

Author Comment

by:ennixo
Comment Utility
* earn, not ear !!
0
 

Expert Comment

by:soumak_05
Comment Utility
How C DLL's can be created from ordinary .c file and how it can be called from an VB6.0 application.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now