Click here to Skip to main content
15,114,302 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,
This is santosh,
While dealing dlls i load dll dynamically from user define folder all information related i got from this code project only.

now i want to call dll fuction function without extern dfination of dll
Posted
Comments
Richard MacCutchan 9-Mar-11 4:02am
   
I do not think that is possible as the information needed to link to the function is not added to the dll file.
Sergey Alexandrovich Kryukov 15-Mar-11 17:13pm
   
Possible, possible, it's just ugly formulation of the Question, OP's confused. Please see my Answer.
--SA
Sergey Alexandrovich Kryukov 15-Mar-11 17:15pm
   
Possible, possible... It's just ugly formulation of the problem, some OP's confusion.
Please see my answer (I'm sure you know how to do it, too).
--SA
Piccadilly Yum Yum 9-Mar-11 5:04am
   
I think its not possible
[no name] 9-Mar-11 6:21am
   
Perhaps he does not want to include the .lib and the header in his project and wants to load a DLL at runtime instead. Long ago I loaded plugins to my programs that way. If I remember right, it started out with calling LoadLibrary() and then trying to obtain a function pointers to the functions I wanted to call.

I would suggest to take a look here:
http://msdn.microsoft.com/en-us/library/ms682599(v=vs.85).aspx
Sergey Alexandrovich Kryukov 15-Mar-11 17:35pm
   
I think you're right, this is what I thought, please see my answer.
--SA
[no name] 16-Mar-11 3:21am
   
Right now I'm at work and have only little time. Perhaps this is helpful: http://home.hiwaay.net/~georgech/WhitePapers/Exporting/Exp.htm

1 solution

This is not about if it is possible or not, this is about the concept and perhaps, terminology.
Your formulation of the problem is misleading, that's why people commented your Question like this.

In fact, I think you can do what you want. I don't need external declaration.

Pay attention: external declaration is itself not a definition but only a declaration. It says: this is a function prototype (without the "function body") used to give a syntax for a call, and the definition will be found elsewhere. Yes, you don't really need it. You can use only a function type.

When you already loaded the DLL you need to do GetProcAddress http://msdn.microsoft.com/en-us/library/ms683212(v=vs.85).aspx[^].

So you have the code, why not calling it? You need just some syntax for call. Here you need a function type. Usually, with C++ you use a lib file and *.h file, but you can do without it. A function type is enough.

For example, this is an example of function type from the code sample of "GetProcAddress Function" MSDN help page http://msdn.microsoft.com/en-us/library/ms683212(v=vs.85).aspx[^]:

C#
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);


Now, look at the full code sample (see reference above). All you need is to declare a pointer to PGNSI (see pGNSI) to obtain the value of this pointer though a type cast from the result of the call to GetProcAddress. When you get non-null result, your are done. Now you can call pGNSI.

I hope it's clear now.

—SA
   
v2
Comments
Richard MacCutchan 15-Mar-11 18:13pm
   
I don't think this is completely true. If the function in the DLL is not declared with __declspec(dllexport) then it will still not be visible to GetProcAddress().
Sergey Alexandrovich Kryukov 15-Mar-11 18:54pm
   
Sorry, what you say is only true for static linking of the DLL (also works with import definition files), not with LoadLibrary. I know for sure.
--SA
Richard MacCutchan 15-Mar-11 19:55pm
   
Not true, I just tested it.
Sergey Alexandrovich Kryukov 15-Mar-11 20:16pm
   
You could have made mistake, did you think about it. What's not working? I tested dynamic load in this way, it works.

Look, if too people try the functionality, and it work in one case and not another, who is right? The problem is that not working code does not disprove the idea. Show you code if you want, then I can show how to make it working... As simple as that.

--SA
Richard MacCutchan 16-Mar-11 5:01am
   
Yes I could have made a mistake but I didn't. I ran my test twice and used Microsoft's Dependency walker to confirm that the function name was not exported without the dllexport.
   
There must be some misunderstanding. In GetProcAddress use, dependency cannot be detected by dependency worker. How about exact code sample by Microsoft I referenced above. Also not working?
--SA
Richard MacCutchan 16-Mar-11 7:39am
   
I think you misunderstand what I am saying. GetProcAddress() will only find functions that are declared with dllexport in their source modules. The sample you point to is finding an address in a Microsoft dll that has been correctly exported, so GetProcAddress() succeeds. However if you add a function to a dll that does not have the exported attributes then it is not added as part of the loader/linker table of the dll and so cannot be found. The issue is simple to reproduce and, as I said before, can be confirmed by the use of dependency walker.
Sergey Alexandrovich Kryukov 16-Mar-11 16:01pm
   
I already suggested in other comment that we only had misunderstanding in terminology. Of course this your comment is absolutely correct. But my answer is correct as well in all parts, and you also should not misunderstand it.

This is because I only discuss the executable which is loading a DLL, not the one which exports a function, which I take for granted, (following OP's Question). Also, I don't discuss every possible meaning of the word "external". I only focus on the "external declaration" (not "definition", by the way!) as it is understood when talking about "extern". This is the only notion OP mentioned. You could not deny "extern" keyword is not needed. The exported attribute you have mentioned is not called "extern" as well, even though it has something to do with the term "external" is more general sense of this word. So if you follow all reasoning with the stricter (and more limited, restricted) terms, you will see that all was correct.

OK. My conclusion is that all our argument was due to the fact that I used more narrow terms of "external" and you tried to interpret it in some wider sense. Our argument was only over terminology. By the way, I did not imagine for a second that you cannot use all forms of library import/export properly. It was only some wording mess.
So what, are we good now?

Thank you.
--SA
Olivier Levrey 16-Mar-11 5:26am
   
I don't understand how this can work. GetProcAddress will work only if the function is exported (through __declspec(dllexpoprt) as Richard said or through the .def file). I just tested it to make sure: I can't get a pointer on my function if the export is missing (GetLastError returns 127: procedure not found). And the GetProcAddress help page itslef mentions it.
   
Wait, exported by what? By the library being loaded, sure. But it does not involve extern declaration. We do not discuss the library being loaded at all. This is given. We only discuss the code using the library. If you use via load library, as OP wanted, you don't need extern declaration but you need function type. That's the only point.

I cannot imagine you don't know how to use LoadLibrary+GetProcAddress, I rather thing there is miscommunication. Hope this comment will clarify it.

--SA
Olivier Levrey 16-Mar-11 5:38am
   
OK I totally agree with you then. extern is not needed at all. I misunderstood the conversation. My english understanding is sometimes limited...
Sorry for that. And have a 5 then because what you say is true and I used it many times!
   
Oh, great. Thank you very much.
Now I'm still in argument with Richard (and of course with OP).
Suspect this is the same kind of misconception, even though Microsoft sample I reference illustrate this thing in full...
--SA
Richard MacCutchan 16-Mar-11 7:40am
   
Yes, you are still confused, please read my comment above.
[no name] 15-Mar-11 23:33pm
   
I thing Richard was true GetProcess get process handle only when we define function if .def file and not for _declspec(dllexport) please not give me alternative
   
Sorry, Richard is not right, and this is not the alternative. You wanted to go along without "external" -- you got it. After all, look at Microsoft code sample from my reference. Validate my Answer is your want.
--SA
Richard MacCutchan 16-Mar-11 7:40am
   
I am right, and you are wrong. Go and test it.
Sergey Alexandrovich Kryukov 16-Mar-11 12:22pm
   
Test what? I tested. OK, can you explicitly answer just one question: will the Microsoft sample, the one I reference, work? I have no idea what are you testing...
I think we're just just having misunderstanding about what to call external or extern.
I only say that the sample is correct and using LoadLibrary+GetProcAddress does not involve external declaration, but you can simply put a different meaning to the word "external".
--SA

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