Testing with DLLIMPORT in C#

S

Safiiru no Baka

Hi thanks for reading this. I'll keep it simple.

In a project I've a MFC DLL:
---------------------------------------------------------------------------------------------
extern "C"
{
__declspec(dllexport) int DoSomethingInC()
{
return 5;
}
}
---------------------------------------------------------------------------------------------

In the same project I have a C# Windows App with the following:

---------------------------------------------------------------------------------------------
[DllImport(@"C:\Documents and Settings\My Documents\Visual Studio
Projects\C_DLL\C_DLL\Release\C_DLL.dll", EntryPoint="DoSomethingInC")]
public static extern int DoSomethingInC();
---------------------------------------------------------------------------------------------

and

---------------------------------------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
double ABC = DoSomethingInC();
MessageBox.Show("Result: "+ABC,"");
}
---------------------------------------------------------------------------------------------

Questions:
1) Any way I can load the DLL without typing the whole address? I cant
seem to get it to load otherwise

2) The messagebox is returning NaN for the ABC variable. I dont know
what is wrong.

thanks again.
 
M

Mattias Sjögren

1) Any way I can load the DLL without typing the whole address? I cant
seem to get it to load otherwise

Have you tried placing it in the application directoy?

The runtime simply uses LoadLibrary to load the DLL, and the search
rules for that are outlined here

http://msdn.microsoft.com/library/en-us/dllproc/base/dynamic-link_library_search_order.asp

2) The messagebox is returning NaN for the ABC variable. I dont know
what is wrong.

Does it make any difference if you set the calling convention to Cdecl
in the DllImport attribute?


Mattias
 
S

Safiiru no Baka

Mattias,

changing the DLL output directory to the application directory fixed
both problems. Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top