How to create DLLs
Posted: Mon Feb 14, 2011 8:09 am
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):
Here's my command window source to call it (main.cpp created as Project | New Workspace | Console | Simple Exec (gcc)):
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?
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;}
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;
}
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?