Calling C++ dll Method from C#

saTech

I am trying to call method available in C++ dll

HRESULT WINAPI TestMethod(
_Out_     BOOL   *isSuccess,
_In_opt_  DWORD  UsernmaeLength,
_Out_opt_ LPWSTR userName );

Wrapper method Which I have written in C# looks like this

        [DllImport("Test.dll", CharSet = CharSet.Unicode, SetLastError = true ,CallingConvention = CallingConvention.StdCall)]
    public static extern int TestMethod (
        IntPtr isSuccess,
        [In, Optional] int UsernmaeLength,
        out string userName
    );

I am calling this method in program

Wrapper. TestMethod (isSuccess, 200, out userName);

I am getting System.AccessViolationException

tried changing the C# wrapper method with

[DllImport("Test.dll", CharSet = CharSet.Unicode, SetLastError = true ,CallingConvention = CallingConvention.StdCall)]
    public static extern int TestMethod (
        bool isSuccess,
        [In, Optional] int UsernmaeLength,
        out string userName
    );
    //Caller
    bool isSuccess = false;
    Wrapper. TestMethod (isSuccess, 200, out userName);

Could you please help me to understand what I am doing wrong here?

Hans Passant
 _In_opt_  DWORD  UsernmaeLength

The SAL annotation is not very useful. What it probably is trying to tell you is that you can pass NULL for the string buffer argument. In which case what you pass for the buffer length doesn't matter. It is not actually [Optional], you'd consider simply passing 0 if you really don't want a string back.

The 3rd argument cannot be String or out since that is an immutable type and the function wants to write into the buffer you pass. It must be StringBuilder. The 2nd argument must be its Capacity. Be sure to make the StringBuilder big enough to fit a user name. If it is not then it isn't very obvious what will happen, hopefully the function then just returns an error code instead of silently truncating string. Test that.

The 1st argument is bool passed by reference, [Out] out bool. Not very likely that it SetLastError, that is only done by winapi functions. It already returns an error code embedded in the HResult. A value less than 0 is an error. Stdcall is the default. Summarizing:

[DllImport("Test.dll", CharSet = CharSet.Unicode)]
public static extern int TestMethod (
    [Out] out bool isSuccess,
    int userNameLength,
    StringBuilder userName
);

Called as:

bool success;
var name = new StringBuilder(666);
int hr = TestMethod(out success, name.Capacity, name);
if (hr < 0) Marshal.ThrowExceptionForHR(hr);

If you still have trouble then you need the help of the author of this code if you cannot debug it yourself. Have a small repro available so he can easily repro the issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a java method from c++ in Android

From Java

Calling C++ dll from Java

From Dev

Calling Rust method from C with array parameters

From Dev

calling C++ method from C code

From Dev

Properly calling C++ functions in DLL from Delphi "Access Violation"

From Dev

Calling C++ method with callback from ObjectiveC

From Dev

Calling a function from a DLL which is developed in C++ from C

From Dev

Calling C++ DLL from C++ and C#

From Dev

C++ calling DLL functions

From Dev

Calling dll implemented JNI from C++

From Dev

How to resolve Error external exception E0434352 on calling a C# dll method from Delphi

From Dev

Call a method in program from dll in C#

From Dev

Calling C++ dll from python

From Dev

Error while calling C DLL function from Python

From Dev

crash on "new" when calling a c++ dll from vb

From Dev

Calling a method in a C dll fails

From Dev

Calling a C++ DLL from C# with unions

From Dev

calling objective c method from callback

From Dev

Calling a C# method from JavaScript

From Dev

Access C# method in DLL from Java

From Dev

Crash when calling c++ dll function from c#

From Dev

c++ calling method from subclass in superclass

From Dev

in C# calling from dll a C function with both wchar and char

From Dev

FileNotFoundException when calling C# dll from C++/CLI

From Dev

Error when calling c++ dll from delphi

From Dev

calling method from a new thread C

From Dev

Calling a C# DLL from Delphi, with array of byte parameter

From Dev

Calling dll from a Win32 Console Application [C]

From Dev

Calling C dll from C#, return types slightly different