Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how can I consume a dll in my c++ code written in netbeans 7.3 ?
Posted
Comments
Sergey Alexandrovich Kryukov 2-Apr-13 1:32am    
Why not asking this question on the page where you asked about creation of DLLs, your previous question. in a comment to an answer or using "Improve question"?
This is almost a re-post. Your next question will be what? How to set a calling conventions? Export/Import a class? A separate question per topic?
—SA
AsthaS 2-Apr-13 2:25am    
I am new to this forum and mind you i wasn't aware that this forum had such norms.
Richard MacCutchan 2-Apr-13 4:12am    
You don't 'consume' a dll (whatever that may mean), you link it to an executable program. If you do not understand such basic issues then you should focus on some simpler programming issues first.
AsthaS 2-Apr-13 5:12am    
Consume is just a word that i used...My purpose for posting this question was just to get my problem solved(which I am not able to find yet) and not to get instructions for what to do and what not to...
PS- don't take it personally..but i really want a solution thats it.
Matthew Faithfull 2-Apr-13 7:06am    
Ahh, I see that I missed your follow up question when I replied to your previous question and suggested posting a follow up. Sorry it doesn't look like that went too well. You need to try something first, implicit linking is easiest, so that you have code to post with your question if you get stuck. It would help to the name of your Dll and the Exe that you are linking it to and a specific function in one that you are trying to call from the other, which way round you want to make the call and where any custom parameter types are defined.

1 solution

I'm guessing you probably didn't want to post your code as a solution but never mind. There are a few odd things here but I don't know for sure if any of them are the cause of the error message you're seeing.
You're using Dynamic/Late/Runtime/Explicit loading of the Dll in this code which is fine, it means that you should be able to run the code in Debug and set a Breakpoint to stop execution before the error occurs. If you can do this there's a very good chance of solving the problem.

The first thing is that hGetProcIDDLL needs to be checked to see if you actually managed to load the Dll, given that you say the Dll is called 'MyDll' and the file name you've tried to load is 'libCppApplication_1' I would say that's unlikely.

Once the Dll is loaded there's the call to GetProcAddress. Your way of casting is slightly unconventional and may cause confusion. I prefer the long hand explicit C++ cast for these scenarios especially for test code.

FARPROC lpfnGetProcessID = GetProcAddress( reinterpret_cast< HMODULE >(hGetProcIDDLL),"demo");


There's one further issue with this related to calling convention. Your code comments state that the function uses __stdall Pascal convention. Confusingly this is stated as for maximum compatability, unfortunately __stdcall has very poor compatability especially between Microsoft and Borland compilers. If the Dll does in fact export this function as __stdcall then the name is likely but not certain to have an "_" automatically prepended to it making it "_demo" or even "__demo". This depends on which compiler you're using on the back end of Netbeans and if it's GCC what options Netbeans is passing through to it.

To avoid these horrible "_" issues either change the Dll to use __cdecl 'C' calling convention or examine the Dll binary with a tool like depends to determine exactly what name the compiler has exported.

Once you get this far and actually have a function pointer for your function the rest should go smoothly. If you get into any trouble remember the simple rule. The module that allocates must be the module that frees In other words don't allocate anything, then pass it, then free it in the module it was passed into.

Happy Dlling
 
Share this answer
 
Comments
AsthaS 5-Apr-13 8:27am    
Thanxx Matthew...After a bit of roaming around the facts..I have finally done it..!!
Matthew Faithfull 5-Apr-13 8:31am    
Congrats. Now your future projects can have great modular architecture.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900