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

0

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
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    USES_CONVERSION;
    DWORD uiReturnVal = -1;
    CString strBuf;
    TCHAR tcBuf[256] = { 0 };

    MBS2WSTR(lpStartIpAddress, tcBuf);

       ........

    return uiReturnVal;
}

And my C# function where I call my DLL:

[System.Runtime.InteropServices.DllImport("C:\\Users\\212721485\\Documents\\CBWatch\\Archive_CBW3\\CBW3SyncFiles\\CBW3SyncFiles\\IEDfinder.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("10.5.47.1");
            StringBuilder test3 = new StringBuilder("10.5.47.153");



            ScanForIed(test1,test2,100,test3);
        }

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 !

c#
c++
dll
dllimport
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