-1

I'm very new to interop and I am having trouble defining the dll imports from a C++ DLL. The documentation for the DLL is as follows:

bool __stdcall __declspec(dllexport) InitHW(char *name, char *model, int& type)

So the code I tried is as follows and it gives a system.AccessViolation exception:

[DllImport("extIO_IC7610.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern bool InitHW(string name, string model, int type);

private unsafe void Initialize()
{
    try
    {
        bool result;

        string name = "Test";
        string model = "Model";
        int type = 3;

        result = InitHW(name, model, type);
    }
    catch (Exception ex)
    {

    }
}

I just realized this is supposed to return data. Could someone please show me the errors in my understanding here? Thanks, Tom

Based on the comments I changed things to look like this:

[DllImport("extIO_IC7610.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)] 
public unsafe static extern bool InitHW(string name, string model, ref int type);

unsafe private void Initialize())
{

    try
    {
        bool result;

        string name = ""; 
        string model = ""; 
        int type = 3;
        result = InitHW(name, model, ref type);

    }
    catch (Exception ex)
    {

    }
}

This still does not work. I now get an error that the stack is unbalanced due to the signatures not matching. I think the strings are done correctly but the &int parameter may still be an issue. Tom

5
  • 2
    I think you need ref int type... Not sure about the rest though I see no need for unsafe Commented Sep 23, 2018 at 19:08
  • Aside from the ref int already mentioned, note that the return type of your native C++ function is the C++ data type bool. Your DLLImport has to specifically account for that (the default marshalling for the C# data type bool is the native WinAPI type BOOL, which is different from the C++ data type bool). Why and how to do this exactly, see here: blogs.msdn.microsoft.com/jaredpar/2008/10/14/…; or see here: stackoverflow.com/questions/4608876/… Commented Sep 23, 2018 at 19:53
  • also make sure you specify the string encoding that your dll expects! Commented Sep 23, 2018 at 20:03
  • Still get errors Commented Sep 23, 2018 at 20:38
  • Getting closer. I need to use CallingConvention.StdCall. That seems to work and I have no errors, but all returned parameters are null. So something is still wrong with the parameters. Commented Sep 23, 2018 at 23:28

1 Answer 1

0

Well I solved it. There were a couple of hints here but nothing really correct. After looking around for the last several hours this is the solution:

    [DllImport("extIO_IC7610.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    [return: MarshalAs(UnmanagedType.I1)] 
    public unsafe static extern bool InitHW(StringBuilder name,  StringBuilder model, int* type);

unsafe void Initialize()
{
           bool result;

            int type = 0;
            var name = new StringBuilder(250);
            var model = new StringBuilder(250);

            result = InitHW(name, model, &type);

            string _name = name.ToString();
            string _model = model.ToString();

}

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

2 Comments

No need for unsafe. Use out int or ref int.
You are right. I do have other functions that require unsafe. I just didn't put them in. Thanks

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.