c# dllimport an attempt was made to load a program with an incorrect format


I want import an external C++ DLL in my C# code. But my following code doesn't work...

I build my C# with "Any CPU" and my C++ DLL is built in 32 bits.

Here my C++ DLL function:

extern "C" DWORD __stdcall ScanForIed(LPSTR lpstrHtmlIedTag, LPSTR lpStartIpAddress, DWORD iNbAddressesToScan, LPSTR lpSelectedAddress)
{// exported function (entry point)
#ifdef _DEBUG
    DWORD uiReturnVal = -1;
    CString strBuf;
    TCHAR tcBuf[256] = { 0 };

    MBS2WSTR(lpStartIpAddress, tcBuf);


    return uiReturnVal;

And my C# function where I call my DLL:

        public static extern int ScanForIed(StringBuilder lpstrHtmlIedTag, StringBuilder lpStartIpAddress, uint iNbAddressesToScan, StringBuilder lpSelectedAddress);

        private void buttonSearchIp_Click(object sender, EventArgs e) {
            StringBuilder test1 = new StringBuilder("CBWatch3");
            StringBuilder test2 = new StringBuilder("");
            StringBuilder test3 = new StringBuilder("");


I try to use "StringBuilder" or "string" for LPSTR argument and "uint" for DWORD argument. And I tried to build my C# program in 32 bits but I have the same error

When I run my program, I have this error:

"An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

Have you some ideas of problem ? Thank's for any helps !

asked on Stack Overflow Sep 25, 2019 by Bastien Cuenot • edited Sep 25, 2019 by Bastien Cuenot

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0