my first dll doesn't work

Started by
19 comments, last by temp_ie_cant_thinkof_name 20 years, 1 month ago
Here are the two projects built seperately:


//myapp.cpp
//a console project 
//trys to link and call dll at run-time

#include <windows.h>
#include <stdio.h>

//I tried extern "C" {} on this, but didn't help
__declspec(dllimport) void myPrintString(char *string);

typedef void (*myPrintStringPtr)(char *string);

void main(void)
{
   HINSTANCE dll = LoadLibrary("mydll");
   if(dll != NULL)
   {
      myPrintStringPtr myPrint =    (myPrintStringPtr)GetProcAddress(dll, "myPrintString");
      if(!myPrint)
      {
         FreeLibrary(dll);
      }
      else 
      {
         myPrint("I'm supposed to see this, but I'm not");
         printf("If you see this, then I got the address of the function, right???");
         FreeLibrary(dll);
      }
}

//mydll.cpp
//I put the output dll into the release or debug directory of myapp.cpp

#include <stdio.h>

__declspec(dllexport) void myPrintString(char *string);

void myPrintString(char *string)
{
   printf("%s \n", string);
}

 
That should compile, but I just made that up for simplicity. No matter what I try to call in my exported-frm-dll function, it won't get called... I tried doing this in Win32 app and in console. I can run it and I can verify that I loaded the library and got the exported function with GetProc, but I can't call it>_< Anyhow, I'm glad I caught these problems now, before I started my actual project around a dll. Edit: why aren't the source tags working? [edited by - temp_ie_cant_thinkof_name on March 21, 2004 5:33:25 PM]
"I study differential and integral calculus in my spare time." -- Karl Marx
Advertisement
EDIT TO ABOVE: Actually, I DID NOT get as far as the printf statement. So, I''m not linking, and I''m not calling GetProc. Does anyone have any idea why? I put the dll in the same directory as the app''s exe, and the system is supposed to search there first...
"I study differential and integral calculus in my spare time." -- Karl Marx
Did you try using "mydll.dll" instead of just "mydll"?
CORRECTION AGAIN: LoadLibrary() is returning success, so I''m linking, but GetProcAddress() is failing. That''s where I need help. I''m going to try ''extern "C" {} again on the imported prototype.
"I study differential and integral calculus in my spare time." -- Karl Marx
quote:Original post by SiCrane
Did you try using "mydll.dll" instead of just "mydll"?


All, the examples I''ve seen show "nameofdll" w/o the externsion. So I guess LoadLibrary() knows what''s meant. But just in case, I''ll try that even though LoadLib() says it''s working, hmmm...
"I study differential and integral calculus in my spare time." -- Karl Marx
Well that was a suggestion for when you said that you LoadLibrary() wasn''t working. What does GetLastError() say after a fail call?
Okay, the extension part in the string (".dll"), doesn''t matter, and the extern "C" doesn''t matter. This is what I''ve deduced. Question: in my test app, I''m sending the file name as a string with space characters in it, because that''s what I named the dll ("My Dll Blah"). Does LoadLibrary() mind the spaces??? I''ll get back to you with GetLastError, hold on.
"I study differential and integral calculus in my spare time." -- Karl Marx
quote:Original post by temp_ie_cant_thinkof_name
Does LoadLibrary() mind the spaces?
I shouldn''t think so, given that Windows supports spaces in filenames generally and that your call to LoadLibrary returns successfully.

I called GetLastError() after GetProcAddress() failure, and it gave the int error value 127, which in MSDN is
ERROR_PROC_NOT_FOUND -- The specified procedure could not be found. -- 127
"I study differential and integral calculus in my spare time." -- Karl Marx
Did you try doing a exported symbol dump from your dll to make sure that the symbol name you''re using is actually the symbol name of your function? It could be an issue with name mangling.

If that is the problem, my personal preferred solution is to add a .def file to when you link the dll.

This topic is closed to new replies.

Advertisement