Importing a C ++ dll into a C # project

I am importing some C ++ dll into a C # project, I am using Visual Studio 2010. I have successfully imported a function that uses the built-in type, however I get an error when I try to figure out the structure. This is a simple example:

C ++ code

typedef long int TDate; typedef struct _TMDYDate { long month; /* In range [1,12] */ long day; /* In range [1-31] */ long year; /* In range [1600-] */ } TMonthDayYear; int JpmcdsDateToMDY (TDate date, /* (I) TDate format */ TMonthDayYear *mdyDate); 

and I translated into C # as:

  [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct TMonthDayYear { public int month; public int day; public int year; } public partial class NativeMethods { [System.Runtime.InteropServices.DllImportAttribute("MyDll.dll", EntryPoint="JpmcdsDateToMDY")] public static extern int JpmcdsDateToMDY(int date, ref TMonthDayYear mdyDate) ; } 

when I try to run a function in my test program, I get this error:

Unhandled exception: System.AccessViolationException: Attempted to read or write protected memory. This often indicates that another memory is corrupted. at CsharpWrapper.NativeMethods.JpmcdsDateToMDY (date Int32, date TMonthDayYear & mdy)

The structure is declared on the stack, and I thought (maybe) about the problem, but I still get the same error, although I have a TMonthDayYear change for the class.

What am I doing wrong?

Thanks for the help.

+4
source share
4 answers

You must use the CallingConvention property in the [DllImport] attribute, this is Cdecl since you did not use __stdcall in the declaration of the built-in function. Although this is wrong, this is not a good explanation for AV. You need to debug the C code, AV assumes a pointer error. Project + Properties, Debug, check "Enable unmanaged code debugging" and set a breakpoint on function C.

Honestly, such a date conversion must be written in pure C #.

+3
source

TDate enter your own long int code, but in managed code, it is represented as int32 instead of int64.

0
source

It may or may not be related, but you need [OutAttribute] in the mdyDate parameter to write to it.

0
source

If you are a familiar user in C ++, you are probably familiar with pointers that access memory directly. Your error looks like a memory read / write protection issue.

This is forbidden by the standard character of C #, and u must put the compiler in unsafe mode.

If you encode an unsafe value in C #, you need to put the code in unsafe mode.

  unsafe { // unsafe things } unsafe class Class1 {} static unsafe void someMethod ( int* cpi, int lngth) {...} 

You also need to check the project configuration ( Build -tab) and check the box " Allow insecure code ."

I apologize if I scroll through some too obvious information. I will also point out that this comment only makes sense if C # will access memory.

0
source

Source: https://habr.com/ru/post/1342281/


All Articles