简体   繁体   中英

Calling c++ DLL from C# Program, unhandled exception

I've done a lot of searching and testing and I've been unable to get this to work correctly. I'm using MVS Express 2013, compiling a c++ win32 DLL that I hope to call from ac# GUI.

On the C++ Side of things, I have a function that I've exported, and it gets passed a struct. The struct originally contained two strings but it seems easier to pass a char array of known size.

C++ Code:

struct runDetails{

    char requestedRuntype[32];
    char filename[32];

};


void __declspec(dllexport) WindowRecreatorCall(runDetails* incomingRunRequests);

c# Code:

Attempting to recreate the Struct to pass in:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
    public struct runDetails{
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string requestedRuntype;
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string filename;
    };

Set up the Dynamic DLL Wrapper:

class CallWindowRecreator
    {
        [DllImport("WindowRecreatorDLL.dll", EntryPoint = "WindowRecreatorCall", CharSet = CharSet.Unicode)]
        public static extern void WindowRecreatorCall(ref runDetails runDetails);
    };

Actual Call to the DLL:

runDetails testing;
testing.requestedRuntype = "Minimize";
testing.filename = "";

CallWindowRecreator.WindowRecreatorCall(ref testing);

As it is right now, I get this error when I attempt the DLL call:

An unhandled exception of type 'System.BadImageFormatException' occurred in WindowRecreatorGUI.exe

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

I've done a lot of googling and code changing and I've learned a lot but I just can't figure this one out. Any tips would be greatly appreciated.

Edit: Changed the code and error recieved

Edit 2: I've changed the C# program from Any CPU to x86 specifically, and now I get this error:

An unhandled exception of type 'System.EntryPointNotFoundException' occurred in WindowRecreatorGUI.exe

Additional information: Unable to find an entry point named 'WindowRecreatorCall' in DLL 'WindowRecreatorDLL.dll'.

And Edit 3 before bed:

I've added an extern c{} around the c++ function. Now I get this error:

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\\Users\\Tom\\workspace\\WindowRecreatorGUI\\WindowRecreatorGUI\\bin\\x86\\Debug\\WindowRecreatorGUI.vshost.exe'.

Additional information: A call to PInvoke function 'WindowRecreatorGUI!WindowRecreatorGUI.CallWindowRecreator::WindowRecreatorCall' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Your native method takes a pointer to the struct.

In C#, that becomes a ref parameter:

[DllImport("WindowRecreatorDLL.dll", EntryPoint = "WindowRecreatorCall", CharSet = CharSet.Unicode)]
public static extern void WindowRecreatorCall(ref runDetails runDetails);

You also need to pass the correct CallingConvention in the attribute, which is probably Cdecl .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM