Solved

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

Posted on 2003-11-23
14
18,444 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
ID: 9805791
extern "C"
{
      export void __stdcall HelloWorld();
}


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



HTH
0
 
LVL 5

Author Comment

by:ennixo
ID: 9805885
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
ID: 9807197
extern "C"
{
  export void __cdecl HelloWorld();
}

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


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

HTH
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Author Comment

by:ennixo
ID: 9807629
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
ID: 9807820
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
ID: 9807852
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
ID: 9807928
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
 
LVL 5

Author Comment

by:ennixo
ID: 9808098
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
ID: 9813496
Listening
0
 
LVL 12

Expert Comment

by:vascov
ID: 9814141
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
ID: 9814917
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
ID: 9818333
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
ID: 9818358
* earn, not ear !!
0
 

Expert Comment

by:soumak_05
ID: 12286026
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 105
conditional code and condition difference 9 83
Zip Folders Using Chilkat Routines 1 41
Problem to App 4 83
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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…

803 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