views:

517

answers:

3

I am currently developing an application for Windows CE on the TI OMAP processor, which is an ARM processor. I am trying to simply call a function in a C++ DLL file from C# and I always get a value of 0 back, no matter which data type I use. Is this most likely some kind of calling convention mismatch? I am compiling the DLL and the main EXE from the same Visual Studio solution.

C# Code Snippet:

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        byte test = LibWrap.test_return();
        MessageBox.Show(test.ToString());
    }
}

public class LibWrap
{
    [DllImport("Test_CE.dll")]
    public static extern byte test_return();
}

C++ DLL Code Snippet:

extern "C" __declspec (dllexport) unsigned char test_return() {
    return 95;
}
+1  A: 

It worked when I changed:

extern "C" __declspec (dllexport) unsigned char test_return() {
    return 95;
}

to

extern "C" __declspec (dllexport) unsigned char __cdecl test_return() {
    return 95;
}

In the DLL code. Why it doesn't assume this when compiled for WinCE is beyond me.

Ben McIntosh
Ben: Probably that's a project settings issue. The default VS2008 Windows Mobile project settings worked fine for me.
Mehrdad Afshari
A: 

Try exporting test_return() as follows:

unsigned char __declspec(dllexport) WINAPI test_return() {
   return 95;
}
Alan
Somewhere WINAPI is being defined as __stdcall where it should have been __cdecl
Ben McIntosh
A: 

Somewhere WINAPI is being defined as __stdcall where it should have been __cdecl

No. WINAPI is defined as __stdcall.

Jeffrey Walton
this should be a comment below Ben's comment, not a new answer.
ctacke