Home > Back-end >  Questions about c # call Delphi DLL return structure
Questions about c # call Delphi DLL return structure

Time:10-13

 
Type
TTestRecord=record
AInt: Integer;
ADouble: Double;
The end;
PTestRecord=^ TTestRecord;

Function RecordTest (var pbufs: PTestRecord) : Integer;

Want to call includes the above function DLL

The practice of I
 
Protected void Button6_Click (object sender, EventArgs e)
{
//1. The dynamic load of c + + Dll
Int hModule=NativeMethod. LoadLibrary (@ "c: \ API. DLL");

//2. Read the function pointer
IntPtr IntPtr=NativeMethod. GetProcAddress call (hModule, "RecordTest");
User_group_list structList=(user_group_list) Marshal. PtrToStructure (intPtr, typeof (user_group_list));

ListStringBuilder sb=new StringBuilder();
Foreach (user_group_t u in listGroupTemp)
{
Sb. Append (u.a Int + "|" + u.a Double + "& lt; Br/& gt;" );
}
Literal1. Text=sb. ToString ();

}






Public static List MarshalPtrToStructArray (IntPtr p, int count)
{
List L=new List (a);
for (int i=0; I & lt; The count. I++, p=new IntPtr (p.T oInt32 () + I * Marshal SizeOf (typeof (T))))
{
T T=(T) Marshal. PtrToStructure (p, typeof (T));
L.A. dd (t);
}
Return the l;
}






Public struct user_group_t
{
//public int id;
//public string name;
Public int aInt.
Public double aDouble;//the balance

}

Public struct user_group_list
{
Public int group_array_count;

Public IntPtr group_array;//pointer to user_group_t type
}







Program go here T T=(T) Marshal. PtrToStructure (p, typeof (T));
Tip try to read or write protected memory, this usually indicates other memory has been damaged,
Then card don't know how to go here
Could you tell me what a great god know to help with the new method or what

CodePudding user response:

Don't have to mark the unsafe?

CodePudding user response:

Returns a Json

CodePudding user response:

IntPtr IntPtr=NativeMethod. GetProcAddress call (hModule, "RecordTest");

IntPtr is the function pointer to a piece of code in the Dll, rather than a structure variable, you want to use PtrToStructure encapsulate the code into what object?

Written in Delphi a Com component to call and access structure, and then call the Com component in c #,

C # and the interaction has a variety of methods of unmanaged code, but through the COM is one of the most reliable,

CodePudding user response:

Try the following method:

 
Using System. The Runtime. InteropServices;
.
Struct TTestRecord
{
Int aInt.
Double aDouble;
}
[DllImport (API. DLL, "EntryPoint=" RecordTest ")]
The static extern unsafe int RecordTest (TTestRecord * * pbufs);

.
//call:
TTestRecord data;
The unsafe
{
TTestRecord * PTR=& amp; The data;
RecordTest (& amp; PTR);
}

CodePudding user response:

Best to add a the packed structure, force the byte alignment, the province of alignment between the heterogeneous problem
  • Related