Page 1 of 1

How to create DLLs

Posted: Mon Feb 14, 2011 8:09 am
by paulkinzelman
I'm trying to make a simple test case to see how to do DLLs and so far haven't been successful, not only on syntax, but also how to create the appropriate workspace/project files. I'm using CL v2.9.0.4684 I was also trying to follow this website example http://www.codeproject.com/KB/cs/usecdlllibincs.aspx as a guide.

Here's my DLL source (testsub.cpp, created as Project | New Workspace | Library | Dynamic Library):

Code: Select all

extern "C"
{  __declspec(dllexport) int addit( int a, int b ); }
int addit (int a, int b) { return a+b;}
Here's my command window source to call it (main.cpp created as Project | New Workspace | Console | Simple Exec (gcc)):

Code: Select all

#include <stdio.h>
[DllImport("testsub.dll")]
public static extern int addit( int a, int b );
int main(int argc, char **argv)
{
  int x=2;  int y=3;
  int answer = addit(x,y);
  printf("hello world, answer is %d\n", answer);
  return 0;
}
I tried creating one workspace (testdlls) and two projects (testcon, testsub) but things don't seem to be working. If somebody could give me some pointers I'd be very grateful. My apologies if the questions seem simple, but I'm just trying to learn how to get this going.

1) I think I've got the two projects building in sequence with the Build | Batch Build... window, is that the right way to build both items - the console app and the dll?

2) When I build everything, the debug area has only the testsub.o object, and no testsub.dll file. It has the dependency files for both main and testsub. And I can't find the main.o anywhere, nor main.exe. How do I get the .dll and .exe file out of the compiles? And to run it, do I put the .dll and .exe on the same area and just run the .exe in a DOS window?

3) How do I pass strings back and forth to a DLL? Is the address space of main in the same context as the dll so I can just pass a pointer back and forth (argument and return value)? Or...?

4) General question about dll stuff - it seems I have to name both sources .cpp, I can't name them .c even if they're otherwise just vanilla C sources, is that correct?

Re: How to create DLLs

Posted: Mon Feb 14, 2011 3:52 pm
by eranif
paulkinzelman wrote:2) When I build everything, the debug area has only the testsub.o object, and no testsub.dll file. It has the dependency files for both main and testsub. And I can't find the main.o anywhere, nor main.exe. How do I get the .dll and .exe file out of the compiles? And to run it, do I put the .dll and .exe on the same area and just run the .exe in a DOS window?
Please post the build log

Eran

Re: How to create DLLs

Posted: Mon Feb 14, 2011 11:20 pm
by paulkinzelman
Here's the build of the DLL:
mingw32-make.exe[1]: Entering directory `C:/apk/adev/ga/testdlls'
gcc -c "C:/apk/adev/ga/testdlls/testsub.cpp" -g -o ./Debug/testsub.o "-I." "-I."
gcc -shared -fPIC -o ./Debug/testsub.so ./Debug/testsub.o "-L."
mingw32-make.exe[1]: Leaving directory `C:/apk/adev/ga/testdlls'

Do I need to rename the .so to be .dll or something? Or is there a change I can make to the project personality to output it as .dll?

As to the main, although it's not on topic for this forum (because it's not directly related to the tool), I've been searching for days and can't find a simple example to show the proper syntax for calling DLLs from C. If somebody could point me to an example similar to what I'm trying to do, I'd greatly appreciate it. Or a suggestion for a good forum in which to ask the syntax questions.

I also realized that the main.cpp is not compiling due to some syntax problem so obviously I have some error in that, but I haven't found a good place on the internet that describes it well.

Re: How to create DLLs

Posted: Mon Feb 14, 2011 11:28 pm
by eranif
paulkinzelman wrote:gcc -c "C:/apk/adev/ga/testdlls/testsub.cpp" -g -o ./Debug/testsub.o "-I." "-I."
Note that you are using 'gcc' to compile a C++ file
paulkinzelman wrote:Do I need to rename the .so to be .dll or something? Or is there a change I can make to the project personality to output it as .dll?
Right click on the project icon in the workspace, and in the 'General' tab rename the
'Output File' field from:

Code: Select all

$(IntermediateDirectory)/$(ProjectName).so
into:

Code: Select all

$(IntermediateDirectory)/$(ProjectName).dll
paulkinzelman wrote:As to the main, although it's not on topic for this forum (because it's not directly related to the tool), I've been searching for days and can't find a simple example to show the proper syntax for calling DLLs from C. If somebody could point me to an example similar to what I'm trying to do, I'd greatly appreciate it. Or a suggestion for a good forum in which to ask the syntax questions.
Try:
http://gamedev.net/index
http://cplusplus.com/forum/
http://www.daniweb.com/forums/forum2.html

Eran

Re: How to create DLLs

Posted: Mon Feb 14, 2011 11:39 pm
by paulkinzelman
Thanks very much! I'll check out those links next.

BTW, for others who might have the same question, it's right click on the Workspace View project icon, then "Settings...", then General.