NO

Author Topic: Loading a DLL?  (Read 3593 times)

ohmycee

  • Guest
Loading a DLL?
« 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!
« Last Edit: August 17, 2011, 04:27:04 PM by ohmycee »

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Loading a DLL?
« Reply #1 on: August 17, 2011, 04:31:58 PM »
There are two ways:
  • add the header and a static .LIB file of the DLL to your project, so the linker knows it is an external library
  • use the LoadLibrary and GetProcAddress Functions to do this manually

EDIT: Nice to see that you changed the message while I was replying :(
---
Stefan

Proud member of the UltraDefrag Development Team

ohmycee

  • Guest
Re: Loading a DLL?
« Reply #2 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.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Loading a DLL?
« Reply #3 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 ;)

BTW, this is not specific to PellesC, this is regular Windows API programming.
---
Stefan

Proud member of the UltraDefrag Development Team

ohmycee

  • Guest
Re: Loading a DLL?
« Reply #4 on: August 17, 2011, 04:49:16 PM »
Hi Stefan,

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

mydll.c code:
Code: [Select]
#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:

Code: [Select]
gcc test.c -o test -L./ -lmydll
test.c code:

Code: [Select]
#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!
« Last Edit: August 17, 2011, 05:07:01 PM by ohmycee »

ohmycee

  • Guest
Re: Loading a DLL?
« Reply #5 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:
Code: [Select]
#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.
Code: [Select]
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:

Code: [Select]
#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:
Code: [Select]
gcc -L. -ltestdll -o test test.c
And everything works.

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

Thanks.
« Last Edit: August 17, 2011, 07:22:29 PM by ohmycee »

ohmycee

  • Guest
Re: Loading a DLL?
« Reply #6 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:
Code: [Select]
; DllTest.def
LIBRARY DllTest.dll
EXPORTS
ReturnNumber
ReturnAnotherNumber

But what are you trying to do here? What's DllTest.c?
Code: [Select]
cc -x -Ze DllTest.c DllTest.def kernel32.lib
And here? DllTestTest?
Code: [Select]
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.