2

I'm Using C# WPF.
I have a C++ test dll as follow:
.h:

extern "C" __delspec(dllexport) void TestMethod();

.cpp file:

extern "C"
{
    __delspec(dllexport) void TestMethod()
    {
        MessageBox(0, L"Test", L"Test", MB_ICONINFORMATION);
    }
}

C# Code:

[DllImport("DllTest.dll", EntryPoint = "TestMethod")]
public static extern void TestMethod();

And when i'm trying to call to TestMethod i got exception:

an attempt was made to load a program with an incorrect format

What i'm doing wrong?
Thanks!


Nitin
  • 18,344
  • 2
  • 36
  • 53
Evyatar
  • 1,107
  • 2
  • 13
  • 36
  • 2
    I have something like `[DllImport("DllTest.dll", CallingConvention = CallingConvention.Cdecl)]`. Specifying the calling convention helps? – ntohl Apr 13 '16 at 08:43

1 Answers1

7

This seems to be 32bit/ 64 bit problem. Seems like your C++ dll and C# calling assembly are built for different platform targets. Try compiling both for the same platform (either x86 or x64) and then calling the function.

Nitin
  • 18,344
  • 2
  • 36
  • 53
  • Thanks!!!.. i edit my post with another question, if i have more than 1 function, what i need to replace instead [DllImport("DllTest.dll", EntryPoint = "TestMethod")] ? – Evyatar Apr 13 '16 at 09:00
  • 1
    @Evyatar best post that as a separate question. We prefer a one-question-per-question format :) – CompuChip Apr 13 '16 at 09:04
  • @CompuChip, yeah.. i ask a new question :P – Evyatar Apr 13 '16 at 09:06