Pelles C forum

C language => Beginner questions => Topic started by: ohmycee on August 17, 2011, 04:05:16 PM

Title: Loading a DLL?
Post by: ohmycee on August 17, 2011, 04:05:16 PM
Hello all,

I have someone else's DLL, and there's a function in it called ReturnNumber(). How do I load the DLL into Pelles C so I can use the function?

Thanks!
Title: Re: Loading a DLL?
Post by: Stefan Pendl on August 17, 2011, 04:31:58 PM
There are two ways:

EDIT: Nice to see that you changed the message while I was replying :(
Title: Re: Loading a DLL?
Post by: ohmycee on August 17, 2011, 04:36:41 PM
I'm so sorry Stefan,  :-[

I felt my question wasn't clear, that's why I changed it. Can I have an example how to use LoadLibrary and GetProcAddress? I'm really lost right now, and very tired. I've been searching the forum for 1 hour. (Pulls some hair out).

I just want to use a simple function from someone else's dll.

Thanks.
Title: Re: Loading a DLL?
Post by: Stefan Pendl on August 17, 2011, 04:41:20 PM
If you would have checked the pages I linked to, you would have found the link to the example called Using Run-Time Dynamic Linking (http://msdn.microsoft.com/en-us/library/ms686944%28VS.85%29.aspx) ;)

BTW, this is not specific to PellesC, this is regular Windows API programming.
Title: Re: Loading a DLL?
Post by: ohmycee on August 17, 2011, 04:49:16 PM
Hi Stefan,

I don't know why it's so complicating.

mydll.c code:

#include <windows.h>

#define DLL_EXPORT __declspec(dllexport)

DLL_EXPORT int ReturnNumber (void)
{
return 98765;
}



For example, using gcc, I can simply "link" mydll.dll as follows, and I can use all the functions in the dll just by calling it:

gcc test.c -o test -L./ -lmydll

test.c code:


#include <stdio.h>
int main(void)
{
printf("Number: %d", ReturnNumber());
return 0;
}


Inside test.c, I simply call the function ReturnNumber(), and it returns a number. Is there something similar in Pelles C?

Thanks!
Title: Re: Loading a DLL?
Post by: ohmycee on August 17, 2011, 07:03:42 PM
Hi timovjl,

I don't know if you're telling me something, or asking me a question?

Let me try my question again... Let's say I have 2 files: testdll.c and test.c

Let's start with testdll.c:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int ReturnNumber (void)
{
return 98765;
}

int ReturnAnotherNumber(void)
{
  return 12345;
}


With GCC, I type this, and I get only a dll file: testdll.dll. I don't get a .def file. Just testdll.dll.
gcc -shared -o testdll.dll testdll.c

At this point, pretend I don't have testdll.c anymore. It's completely gone. I can't make the .lib file. All I have is testdll.dll, and I know there are 2 functions in it.

To use testdll.dll, I write this in test.c:

#include <stdio.h>

int main (int argc, char **argv)
{
  printf("Test: %d\n", ReturnNumber()); //notice I can use the function simply by calling it, no need to loadlibrary or getprocaddress
  printf("Test: %d", ReturnAnotherNumber()); //same thing here
  return 0;
}


To compile the code, I type:
gcc -L. -ltestdll -o test test.c

And everything works.

My question is, can I do this in Pelles C?

Thanks.
Title: Re: Loading a DLL?
Post by: ohmycee on August 17, 2011, 08:21:35 PM
Hi timovjl,

Thanks for trying to help me understand, but I'm even more confused now.  :-[

Would you please take me through your code step by step?

This, I understand:
; DllTest.def
LIBRARY DllTest.dll
EXPORTS
ReturnNumber
ReturnAnotherNumber


But what are you trying to do here? What's DllTest.c?
cc -x -Ze DllTest.c DllTest.def kernel32.lib

And here? DllTestTest?
cc -x DllTestTest.c kernel32.lib DllTest.lib

Remember, we don't have the sourcecode for the DLL anymore. It's gone. We only have the dll. So we can't possibly have the .lib file.

Thanks all.