Home > Net >  C # call DLL C, how to transform a complex structure pointer passed as an argument
C # call DLL C, how to transform a complex structure pointer passed as an argument

Time:11-25

In c + + c # call DLL, how to transform a complex structure pointer passed as a parameter:
Structure is as follows:
 struct TagPoint 
{
int x;
Int y;
};
Struct TagRect
{
int x;
Int y;
Int width;
Int Height;
};
Struct TagInfos
{
Char * name;
PointList TagPoint *;
TagRect the rect.
Int pointCount;
};
Struct FSize
{
Int width;
Int height;
Int the depth;
};
Struct TagImageInfos
{
Char * path;
TagInfos * tagList;
FSize imageSize;
Bool isLable;
Int tagCount;
};

 [System. The Runtime. InteropServices. StructLayoutAttribute (System. The Runtime. InteropServices. LayoutKind. Sequential)] 
Public struct TagPoint
{

///int
Public int x;

///int
Public int y;
}

[System. The Runtime. InteropServices. StructLayoutAttribute (System. The Runtime. InteropServices. LayoutKind. Sequential)]
Public struct TagRect
{

///int
Public int x;

///int
Public int y;

///int
Public int width;

///int
Public int Height;
}

[System. The Runtime. InteropServices. StructLayoutAttribute (System. The Runtime. InteropServices. LayoutKind. Sequential)]
Public struct TagInfos
{

///char *
[System. The Runtime. InteropServices. MarshalAsAttribute (System. The Runtime. InteropServices. UnmanagedType. LPStr)]
Public string name;

///TagPoint * *
The public System. IntPtr pointList;

///TagRect
Public TagRect the rect.

///int
Public int pointCount;
}

[System. The Runtime. InteropServices. StructLayoutAttribute (System. The Runtime. InteropServices. LayoutKind. Sequential)]
Public struct FSize
{

///int
Public int width;

///int
Public int height;

///int
Public int the depth;
}

[System. The Runtime. InteropServices. StructLayoutAttribute (System. The Runtime. InteropServices. LayoutKind. Sequential)]
Public struct TagImageInfos
{

///char *
[System. The Runtime. InteropServices. MarshalAsAttribute (System. The Runtime. InteropServices. UnmanagedType. LPStr)]
Public string path;

///TagInfos * *
The public System. IntPtr tagList;

///FSize
Public FSize imageSize;

///a Boolean
Public bool isLable;

///int
Public int tagCount;
}


From you to send a TagImageInfos structure to c + +, c # problems at present is the inside of the structure pointer the value problems
Struggle for several days. Still not fully resolved, the great god please help to provide a train of thought or solution, thank you!

CodePudding user response:

I think you should need a tool to help you to simplify work,

Baidu "interopsignaturetoolkit" this is a tool, although tool translation how much need to change, but at least 80% of the work can simplify your

If there is other h head with "swig" can also, but swig translation thing is too heavy, so I don't recommend commonly, unless there is a super large amount of translation work to do, it is still recommended that use interopsignaturetoolkit a a translation to insurance

CodePudding user response:

reference 1st floor wanghui0380 response:
I think you should need a tool to help you to simplify work,

Baidu "interopsignaturetoolkit" this is a tool, although tool translation how much need to change, but at least 80% of the work can simplify your

If there is other h head with "swig" can also, but swig translation thing is too heavy, so I don't recommend commonly, unless there is a super large amount of translation work to do, it is still recommended that use interopsignaturetoolkit a a translation to the insurance point of


Thanks to remind, the results of the c # code is already using interopsignaturetoolkit, is also to ensure that both ends of the c + + and c # code type and structure

CodePudding user response:

I understand your question about
Your estimate is///TagPoint * * find a trifle stuck

This thing is actually the expression of an array, is TagPoint [], of course, because we can't determine exactly how much an array, so can't direct definition, so adopting inptr receive

According to pointCount get again after receiving the array size, and then copy to deserve bytes into memory (pointCount * sizeof (TagPoint))

CodePudding user response:

If not he preached to you, is you to his

In fact is
TagPoint [] TagPoints=new TagPoint [20]

PointList=Marshal. UnsafeAddrOfPinnedArrayElement (TagPoints, 0)

Which is the first element of the array pointer,

CodePudding user response:

The
reference 3 floor wanghui0380 response:
I understand your question about
Your estimate is///TagPoint * * find a trifle stuck

This thing is actually the expression of an array, is TagPoint [], of course, because we can't determine exactly how much an array, so can't direct definition, so adopting inptr receive

According to pointCount get again after receiving the array size, and then copy to deserve bytes into memory (pointCount * sizeof (TagPoint))


A great god, is the problem here: TagInfos and TagPoint * * * * these two points, unknown size, are in accordance with the pointer

Before the test under the situation that:
1. Spread a TagPoint [] (substitute IntPtr) to the c + +, c + + side pointer (TagPoint *) receiving use level, the result is wrong; C + + end with secondary pointer (TagPoint * *), the result is a step in the right;
2. Pass a TagInfos [] (substitute IntPtr,) to the c + +, c + + side TagInfos TagPoint inside structure is defined as the secondary pointer, and then use the secondary pointer (TagInfos * *), the result is wrong; Then consulted a great god, and say it's memory of discontinuous problems (custom arrays into the problem of IntPtr), is disposed of in accordance with the method of the great god provided, the result is also ok

3. Now want to send a TagImageInfos object to c + + side (TagImageInfos structure containing an array of TagInfos TagInfos inside each element of the array contains a TagPoint arrays), no matter how to change the current c + + client receives the results are wrong

Should the current problems and the second test problems, but don't know how to deal with to solve it, do so roughly

CodePudding user response:

The
reference 4 floor wanghui0380 reply:
if not he preached to you, is you to his

In fact is
TagPoint [] TagPoints=new TagPoint [20]

PointList=Marshal. UnsafeAddrOfPinnedArrayElement (TagPoints, 0)

That is the first element of the array pointer,


The great spirit of this method can solve the transmitting TagPoint [] (c + + -- -- -- -- -- & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related