[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 18527
  • Last Modified:

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

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
ennixo
Asked:
ennixo
1 Solution
 
vascovCommented:
extern "C"
{
      export void __stdcall HelloWorld();
}


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



HTH
0
 
ennixoAuthor Commented:
thanx a lot ! no more decoration !

but my exports are still twins :
HelloWorld
HelloWorld@0

can't i get only HelloWorld alone ?
0
 
vascovCommented:
extern "C"
{
  export void __cdecl HelloWorld();
}

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


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

HTH
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ennixoAuthor Commented:
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
 
vascovCommented:
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
 
ennixoAuthor Commented:
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
 
vascovCommented:
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
 
ennixoAuthor Commented:
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
 
Dang123Commented:
Listening
0
 
vascovCommented:
Hi, i'll have to download the compiler and play with it.

(downloading as we speak)
0
 
vascovCommented:
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
 
ennixoAuthor Commented:
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
 
ennixoAuthor Commented:
* earn, not ear !!
0
 
soumak_05Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now