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!


1
  • 2
    I have something like [DllImport("DllTest.dll", CallingConvention = CallingConvention.Cdecl)]. Specifying the calling convention helps? Commented Apr 13, 2016 at 8:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 best post that as a separate question. We prefer a one-question-per-question format :)
@CompuChip, yeah.. i ask a new question :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.