loadlibrary c++ ?

Programming, for all ages and all languages.
Post Reply
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

loadlibrary c++ ?

Post by Sam111 »

Ok , this compiles and links fine but when I run it I get the program crashing at the line

Code: Select all

int intMyReturnVal = function1(1, 5);
The code is this

Code: Select all

#include <cstdlib>
#include <stdio.h>
#include <windef.h>
#include <windows.h>
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {


  /* get handle to dll */
   HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\netbeansWorkSpace\\test2dll\\dist\\Debug\\MinGW-Windows\\libtest2dll.dll");

   /* get pointer to the function in the dll*/
   FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"function1");

   /*
      Define the Function in the DLL for reuse. This is just prototyping the dll's function.
      A mock of it. Use "stdcall" for maximum compatibility.
   */
   typedef int (__stdcall * pICFUNC)( int , int);

   pICFUNC function1;
   function1 = pICFUNC(lpfnGetProcessID);

   /* The actual call to the function contained in the dll */
   printf( "got passed before dll value " ) ;
   int intMyReturnVal = function1(1, 5);
    printf( "got passed retreviing value " ) ;
   /* Release the Dll */
   FreeLibrary(hGetProcIDDLL);

   /* The return val from the dll */
   printf( "the number from the dll is = %d" , intMyReturnVal ) ;
   return 0;

}


When I created the dll I used this as a header file

Code: Select all

#ifndef DLLHEADER_H
#define	DLLHEADER_H
extern "C" {
__declspec(dllexport) int __stdcall function1( int i , int j );
__declspec(dllexport) int __stdcall function2( int i , int j );

__declspec(dllexport) int __stdcall function3(int i , int j) ;

}
#endif	/* DLLHEADER_H */
And the cpp for the dll is this

Code: Select all

#include "dllheader.h"

__declspec(dllexport) int __stdcall function1( int i , int j )
{

    return i + j ;

}

__declspec(dllexport) int __stdcall function2( int i , int j )
{

    return i - j ;

}

__declspec(dllexport) int __stdcall function3( int i , int j )
{

    return i * j ;

}

The dll was created with no errors or warnings. So I don't really understand why it is crashing?
when i try to run the program and call the function in the library using loadlibrary ,...etc
Anybody know?

Is this some cdel or stdcall incompatibility issue because other then the calling convention I don't know what could be going wrong.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: loadlibrary c++ ?

Post by Combuster »

Did you check the return values for LoadLibrary and GetProcAddress? Did you grab an useful tool like dependency walker yet?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: loadlibrary c++ ?

Post by Sam111 »

it fails at this line returns null

Code: Select all

 FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"function1");
When I do a get last error I get error number 127 which is
The error is 127 ("The specified procedure could not be found.")

So I am unsure I compiled the above code into a dll isn't the name for

Code: Select all

__declspec(dllexport) int __stdcall function1( int i , int j );
function1?

extern "C" {} should have taken out any mangled name issues.

Could it be because "function1" is not a wide character string w_char * as opposed to char *.
I got no warnings leading me to believe it is that character encoding.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: loadlibrary c++ ?

Post by JamesM »

You've only extern "C"'d the declarations, not the definitions, so actually you're defining functions with C++ name mangling and declaring them without.

Code: Select all

extern "C" __declspec(dllexport) int __stdcall function1( int i , int j )
{
    return i + j ;
}
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: loadlibrary c++ ?

Post by gerryg400 »

I think your naming is wrong. Read this ==> http://www.willus.com/mingw/yongweiwu_stdcall.html
If a trainstation is where trains stop, what is a workstation ?
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: loadlibrary c++ ?

Post by Sam111 »

I have tried _function1@8 instead of function1 still same error?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: loadlibrary c++ ?

Post by JamesM »

berkus wrote:
JamesM wrote:You've only extern "C"'d the declarations, not the definitions, so actually you're defining functions with C++ name mangling and declaring them without.

Code: Select all

extern "C" __declspec(dllexport) int __stdcall function1( int i , int j )
{
    return i + j ;
}
Not really, he includes the header file with right declarations.
That's strange, I always thought that you always had to declare *and* define the function extern C (else the compiler might think you were referring to two different functions). A quick test has proved me wrong.

I learned something today :/

Cheers,

James
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: loadlibrary c++ ?

Post by Sam111 »

never mind that was the microsoft name mangling convention the Minwin was without the underscore.
Thanks for the link plus. All set now.

Other then using a def file is their any way you can get a dll's functions not to use name mangleing and just have the function name used at compile time to be the actually name called for it in the dll?

Basically how do you turn of this name mangling (convention ) and just use the exact name.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: loadlibrary c++ ?

Post by JamesM »

Sam111 wrote:never mind that was the microsoft name mangling convention the Minwin was without the underscore.
Thanks for the link plus. All set now.

Other then using a def file is their any way you can get a dll's functions not to use name mangleing and just have the function name used at compile time to be the actually name called for it in the dll?

Basically how do you turn of this name mangling (convention ) and just use the exact name.
If that was indeed the problem, my solution may still stand. Did you try putting extern "C" before the function *definition* as well as the declaration?
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: loadlibrary c++ ?

Post by Sam111 »

Yes , simple doesn't work you have to have the function name
function1@8

putting extern before doesn't do anything more it doesn't affect it.
You can use extern or not it has to be function1@8 to access function1.

Does anybody no without using .def files how you can get the names to be the same as how they are declared in the header.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: loadlibrary c++ ?

Post by gerryg400 »

Does anybody no without using .def files how you can get the names to be the same as how they are declared in the header.
Can't you use __cdecl ?
If a trainstation is where trains stop, what is a workstation ?
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: loadlibrary c++ ?

Post by Sam111 »

gcc -fno-leading-underscore
That is cool but what about if you are developing in Visual Studio's is their a compiler switch for that?

Yes in most cases I can use cdecl but in some case's it has to be stdcall so if it has to be stdcall is their any other then def files to use?
Is their a way to turn of this name mangling and just use the exact function name???

Also I am looking for a fortran compiler that would work with the Visual studio's PE/COFF object file format.
Since I need to compiler fortran files into .o or .obj files and then beable to link them with microsoft's LINK compiler.

gfortran , g77 ,gnu compiler don't support that file format I would believe.

Thanks for your help
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: loadlibrary c++ ?

Post by Firestryke31 »

Might I ask why you're so anti-.def file? Their entire purpose is to keep you from having to go through this. IIRC there are tools out there that will take a .dll and generate the .def and .lib files to use them in VS.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: loadlibrary c++ ?

Post by Sam111 »

well, partly because I don't really know how to use .def . Do I just create the .def file and use some linker switch to use that def while linking.

Would I have to get rid of the dllexport statements in the code because the def file would be doing it.... ?

If somebody can explain how to specify in the compiler/linker setting to use a specific .def file I created then that would be excellent.

Also any word on a fortran compiler.
Post Reply