Hello
I need help to pass the following structure from c# to c++ DLL.
C++ Method
C++ Structure DeviceParameters
typedef struct _DeviceParameters
{
BOOL bMICREnable;
UINT nMICRFont;
BOOL bMICRSaveSamples;
UINT nMICRSpaces;
char cRejectSymbol;
UINT nReserved;
BOOL bReserved;
IMAGE_PROPERTIES ImagePropertiesFront1;
IMAGE_PROPERTIES ImagePropertiesFront2;
IMAGE_PROPERTIES ImagePropertiesRear1;
IMAGE_PROPERTIES ImagePropertiesRear2;
SNIPPET_PROPERTIES SnippetProperties[10];
BOOL bPrintEnable;
BOOL bOneDoc;
UINT nFeedingMode;
}
typedef struct _ImageProperties
{
UINT Format; UINT Paging; UINT Resolution; UINT ColorDepth; UINT Threshold; }
IMAGE_PROPERTIES;
typedef struct _SnippetProperties
{
BOOL Enable; BOOL Front; Snippet Properties; }
SNIPPET_PROPERTIES;
typedef struct _Snippet
{
UINT Xposition;
UINT Yposition;
UINT Width;
UINT Height;
UINT Orientation;
UINT Color;
UINT Compression;
BOOL Millimeters;
}
Snippet;
---------------------------------------------------------------------------------------\\
What I have tried:
C# Method (I read first Post and changeDeviceParam to IntPtr)
[DllImport(VisionAPI_PATH, EntryPoint = "?SetDeviceParameters@@YGHKU_DeviceParameters@@@Z")]
static extern public bool SetDeviceParameters(uint DeviceID, IntPtr DeviceParam);
C# Structure ( I converted the structures to IntPtr )
public class _DeviceParameters
{
public int bMICREnable;
public int nMICRFont;
public int bMICRSaveSamples;
public int nMICRSpaces;
public char cRejectSymbol;
public int nReserved;
public int bReserved;
public IntPtr ImagePropertiesFront1;
public IntPtr ImagePropertiesFront2;
public IntPtr ImagePropertiesRear1;
public IntPtr ImagePropertiesRear2;
[MarshalAs(UnmanagedType.SafeArray)]
public IntPtr[] SnippetProperties;
public int bPrintEnable;
public int bOneDoc;
public int nFeedingMode;
}
public class _ImageProperties
{
public int Format;
public int Paging;
public int Resolution;
public int ColorDepth;
public int Threshold;
}
public class _SnippetProperties
{
public int Enable;
public int Front;
public IntPtr Properties;
}
public class _Snippet
{
public int Xposition;
public int Yposition;
public int Width;
public int Height;
public int Orientation;
public int Color;
public int Compression;
public int Millimeters;
}
---------------------------------------------------------------------------------------\\
I use a function that transforms the structure into IntPtr <T> is Type and SourceContent y Content of Structure
private IntPtr ToIntPtr<T> (Object SourceContent)
{
int iSize = Marshal.SizeOf(typeof(T));
IntPtr pTestStruct = Marshal.AllocHGlobal(iSize);
var Structure = (T) SourceContent;
Marshal.StructureToPtr(SourceContent, pTestStruct, false);
return pTestStruct;
}
Function to set and return parameters
private _DeviceParameters Parameters()
{
_DeviceParameters m_Parameters = new _DeviceParameters();
m_Parameters.bMICREnable = 1;
m_Parameters.bMICRSaveSamples = 2;
m_Parameters.nMICRFont = 2;
m_Parameters.nMICRSpaces = 0;
m_Parameters.cRejectSymbol = '?';
m_Parameters.nReserved = 0;
m_Parameters.bReserved = 0;
_ImageProperties ImagePropertiesFront1 = new _ImageProperties();
ImagePropertiesFront1.Format = 2;
ImagePropertiesFront1.Paging = 0;
ImagePropertiesFront1.Resolution = 200;
ImagePropertiesFront1.ColorDepth = 256;
ImagePropertiesFront1.Threshold = 75;
m_Parameters.ImagePropertiesFront1 = ToIntPtr < _ImageProperties > (ImagePropertiesFront1);
_ImageProperties ImagePropertiesFront2 = new _ImageProperties();
ImagePropertiesFront2.Format = 1;
ImagePropertiesFront2.Paging = 0;
ImagePropertiesFront2.Resolution = 200;
ImagePropertiesFront2.ColorDepth = 256;
ImagePropertiesFront2.Threshold = 3;
m_Parameters.ImagePropertiesFront2 = ToIntPtr < _ImageProperties > (ImagePropertiesFront2);
_ImageProperties ImagePropertiesRear1 = new _ImageProperties();
ImagePropertiesRear1.Format = 2;
ImagePropertiesRear1.Paging = 0;
ImagePropertiesRear1.Resolution = 200;
ImagePropertiesRear1.ColorDepth = 256;
ImagePropertiesRear1.Threshold = 75;
m_Parameters.ImagePropertiesRear1 = ToIntPtr < _ImageProperties > (ImagePropertiesRear1);
_ImageProperties ImagePropertiesRear2 = new _ImageProperties();
ImagePropertiesRear2.Format = 1;
ImagePropertiesRear2.Paging = 0;
ImagePropertiesRear2.Resolution = 200;
ImagePropertiesRear2.ColorDepth = 256;
ImagePropertiesRear2.Threshold = 3;
m_Parameters.ImagePropertiesRear2 = ToIntPtr < _ImageProperties > (ImagePropertiesRear2);
m_Parameters.bPrintEnable = 0;
m_Parameters.bOneDoc = 0;
m_Parameters.nFeedingMode = 1;
_SnippetProperties SnippetP = new _SnippetProperties();
SnippetP.Enable = 0;
_Snippet Snippet = new _Snippet();
SnippetP.Properties = ToIntPtr < _Snippet > (Snippet);
_SnippetProperties[] SnippetProperties = new _SnippetProperties[10];
m_Parameters.SnippetProperties = new IntPtr[10];
for (int i = 0; i < 10; i++)
{
SnippetProperties[i] = SnippetP;
m_Parameters.SnippetProperties[i] = ToIntPtr < _SnippetProperties > (SnippetProperties[i]);
}
return m_Parameters;
}
And finally call the function and pass IntPtr (DLL result: "Wrong device parameter").
If structure _DeviceParameters is passed without transforming to IntPtr the APP closes unexpectedly.
SetDeviceParameters( DeviceID, ToIntPtr < _DeviceParameters > (Parameters()) )
------------------------------------------------------------------------------------------------------------------------------------------------\\
If someone can help me, thanks.