Home > Net >  C # call C compiled DLL cannot be marshaling "parameter # 1:" invalid managed/unmanaged ty
C # call C compiled DLL cannot be marshaling "parameter # 1:" invalid managed/unmanaged ty

Time:12-10

 
//declare
[DllImport (" RTDBInterface. DLL, "EntryPoint=" AppendRTTagDataByTagName CharSet=CharSet. Ansi, CallingConvention=CallingConvention. Cdecl)]
Private static extern int AppendRTTagDataByTagName ([MarshalAs (UnmanagedType. AsAny)] InsertData_struct dt);

//simple code
String [] veParams=new String [4].
VeParams [0]="D: \ \ vestore \ ";
VeParams [1]="C: \ \ Windows \ \ SysWOW64 \ ";
VeParams [2]="";
VeParams [3]="";
InitConnect (veParams, 1000000);
InsertData_struct dt=new InsertData_struct ();
Dt. Type=0;
Dt. Status=1;
Dt. Time=1607435725;
Dt. Value=https://bbs.csdn.net/topics/990.99;
Dt. PointName="dcs01. TEST0000";;
Int rs4=AppendRTTagDataByTagName (dt);
Enclosing listBox1. Items. Add (" wrote: "+ rs4 +" - & gt; "+ dt. Value);
//c + + struct
Typedef struct InsertData_struct {
The int type.//to zero for the switch quantity, 1 for analog quantity
Int the status;//write some records the status of the
A double value.//write some records the value of the
Long time;//write some records of time, s
Char pointName [RTDB_TAGNAME_LENGTH];//write some full name
} InsertData;

//c # structure
Public struct InsertData_struct {
Public int type.//to zero for the switch quantity, 1 for analog quantity
Public int the status;//write some records the status of the
Public double value;//write some records the value of the
Public long time;//write some records of time, s
Public string pointName;//write some full name
};

//////////////////////////////////////////////////////////////////////
Anomalies appear the following error

CodePudding user response:

Int is int32, c # and c + + is int16, you need according to the relationship between c # and c + + type is modified, not only the int, char * c + + generally in c # to use StringBuilder instead. To search a type table under reference.

CodePudding user response:

reference 1st floor ziqi0716 response:
int is int32, c # and c + + is int16, you need according to the relationship between c # and c + + type is modified, not only the int, char * c + + generally in c # to use StringBuilder instead. To search a type table under reference.

The part is to modify the structure?

CodePudding user response:

reference peach ripe $_ $2 floor response:
Quote: refer to 1st floor ziqi0716 response:
int is int32, c # and c + + is int16, you need according to the relationship between c # and c + + type is modified, not only the int, char * c + + generally in c # to use StringBuilder instead. To search a type table under reference.

The part is to modify the structure?
that's right

CodePudding user response:

reference ziqi0716 reply: 3/f
Quote: refer to the second floor peach ripe $_ $response:
Quote: refer to 1st floor ziqi0716 response:
int is int32, c # and c + + is int16, you need according to the relationship between c # and c + + type is modified, not only the int, char * c + + generally in c # to use StringBuilder instead. To search a type table under reference.

The part is to modify the structure?
right


Only an increase in
 [MarshalAs (UnmanagedType. LPStr)] 

The code will be an error "cannot be marshaling parameter" # 1 ": invalid managed/unmanaged type combination (the value type must be paired with Struct),"
 
[DllImport (" RTDBInterface. DLL, "EntryPoint=" AppendRTTagDataByTagName CharSet=CharSet. Ansi, CallingConvention=CallingConvention. Cdecl)]
Private static extern int AppendRTTagDataByTagName ([MarshalAs (UnmanagedType. LPStr)] InsertData_struct dt);

Structure increases the
 
Public struct InsertData_struct {
[MarshalAs (UnmanagedType. U4)]
Public int the status;
[MarshalAs (UnmanagedType. U4)]
Public int type.
[MarshalAs (UnmanagedType. The R8)]
Public double value;
[MarshalAs (UnmanagedType. U8)]
Public long time;
[MarshalAs (UnmanagedType. LPTStr)]
Public string pointName;
};

  •  Tags:  
  • C#
  • Related