Hi,
This is an answer, not a question. I hope it helps you since i was
messing around for ages trying to get information on this:
For anyone else wanting to create a test c++nativedll/c# project follow
these instructions...
1) Create a new 'Visual C++ - Win32 Console Project' - call it 'test'
1b) When the wizard pops up, click the 'Application Settings' link on
the left
1c) Select 'DLL' under 'Application Type'
1d) Click 'Finish' (don't worry, everything remains in English)
2) In test.cpp you will need to ADD the following code at the bottom of
the auto-generated stuff:
//START
struct MyTestStruct
{
int iSomeNumber;
int iAnotherNumber;
};
extern "C" __declspec(dllexport) void
DoSomethingWithStruct(MyTestStruct* mts);
void DoSomethingWithStruct(MyTestStruct* mts)
{
if (mts != NULL)
{
mts->iSomeNumber = 12345;
mts->iAnotherNumber = mts->iAnotherNumber + 67890;
}
}
//END
That's the dll sorted. As you can see, DoSomethingWithStruct just sets
the value of the referenced structure's members.
3) Now File Add Project New Project 'Visual C# - Windows
Application'
3b) Add a button to your form - leave it named 'button1'
3c) Add the button click event handler
3d) Paste the code below OVER the entire event handler 'private void
button1_Click( ... ) { ... }':
//START
struct MyTestStruct
{
int iSomeNumber;
int iAnotherNumber;
};
[System.Runtime.InteropServices.DllImport(@"REPLACE
_THIS_WITH_THE_PATH_TO_YOUR_TEST.DLL_FILE\test\Deb ug\test.dll")]
private static extern void DoSomethingWithStruct(ref MyTestStruct
theStruct);
private void button1_Click(object sender, System.EventArgs e)
{
MyTestStruct mts = new MyTestStruct();
DoSomethingWithStruct(ref mts);
}
//END
3e) change the path where it says
REPLACE_THIS_WITH_THE_PATH_TO_YOUR_TEST.DLL_FILE to point to your dll.
4) Set the C# Windows Application as the startup project
5) Set a breakpoint so you can see that mts has indeed been changed
6) Run the project.
Shamelessly adapted from
http://blogs.msdn.com/jonathanswift/...02/780637.aspx, but
with some vital information added thanks to b0b - you know who you are.
Regards,
James Randle.
Also check out jonathan's followup article
http://blogs.msdn.com/jonathanswift/...23002900_.aspx
which eliminates the need to specify the location of the dll in the
DllImport attribute. Excellent work :).