Click here to Skip to main content
15,886,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I need help to pass the following structure from c# to c++ DLL.

C++ Method
C++
//-------------------------------------------------------------------------------------------------\\
     BOOL SetDeviceParameters( DWORD DeviceID, DeviceParameters  DeviceParam );
//---------------------------------------------------------------------------------------------------------------\\
C++ Structure DeviceParameters
C++
//---------------------------------------------------------------------------------------
typedef struct _DeviceParameters
{
	// MICR
	// MICR Enabling
	BOOL bMICREnable;
	// MICR Font(0=CMC7  1=E13B  2=AUTO, Default: E13B)
	UINT nMICRFont;
	// MICR Waveform File Saving Enabling
	BOOL bMICRSaveSamples;
	// Spaces management (0=NONE  1=SINGLE  2=ALL, Default: NONE)
	UINT nMICRSpaces;

	// Micr+ Library Parameters
	// Reject Symbol Character (Default: '?')
	char cRejectSymbol;
	// Reserved, MUST BE 0
	UINT nReserved;
	// Reserved, MUST BE FALSE
	BOOL bReserved;

	// IMAGE
	// First Front Image Properties
	IMAGE_PROPERTIES ImagePropertiesFront1;
	// Second Front Image Properties
	IMAGE_PROPERTIES ImagePropertiesFront2;
	// First Rear Image Properties
	IMAGE_PROPERTIES ImagePropertiesRear1;
	// Second Rear Image Properties
	IMAGE_PROPERTIES ImagePropertiesRear2;

	// SNIPPETS
	SNIPPET_PROPERTIES SnippetProperties[10];

	// PRINTER
	// Ink-Jet Enabling
	BOOL bPrintEnable;

	// WORKING MODE
	// One Document feeding option
	BOOL bOneDoc;

	// Feeding options (HOPPER FEED = 0, DROP FEED = 1)
	UINT nFeedingMode;

}
// DeviceParameters;

//---------------------------------------------------------------------------------------
typedef struct _ImageProperties
{
	UINT Format;	// Compression Format
	UINT Paging;	// Single-pagefile or multi page TIFF file
	UINT Resolution;	// Resolution (dpi)
	UINT ColorDepth;	// 16, 64 or 256 gray levels
	UINT Threshold;	// JPG quality Factor or G4 threshold
}
IMAGE_PROPERTIES;

//---------------------------------------------------------------------------------------
typedef struct _SnippetProperties
{
	BOOL Enable;	// Snippet Enabling
	BOOL Front;	// TRUE if the Snippet is on the Front
	Snippet Properties;	// Snippet Properties Struct (see Above)
}
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)
C#
//------------------------------------------------------------------------------------------------------------------------------------------------
    [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 )
C#
//---------------------------------------------------------------------------------------
public class _DeviceParameters
{
	// MICR
	// MICR Enabling
	public int bMICREnable;
	// MICR Font(0=CMC7  1=E13B  2=AUTO, Default: E13B)
	public int nMICRFont;
	// MICR Waveform File Saving Enabling
	public int bMICRSaveSamples;
	// Spaces management (0=NONE  1=SINGLE  2=ALL, Default: NONE)
	public int nMICRSpaces;

	// Micr+ Library Parameters
	// Reject Symbol Character (Default: '?')
	public char cRejectSymbol;
	// Reserved, MUST BE 0
	public int nReserved;
	// Reserved, MUST BE FALSE
	public int bReserved;

	// IMAGE
	// First Front Image Properties
	public IntPtr ImagePropertiesFront1;
	// Second Front Image Properties
	public IntPtr ImagePropertiesFront2;
	// First Rear Image Properties
	public IntPtr ImagePropertiesRear1;
	// Second Rear Image Properties
	public IntPtr ImagePropertiesRear2;

	// SNIPPETS
[MarshalAs(UnmanagedType.SafeArray)]
	public IntPtr[] SnippetProperties;

	// PRINTER
	// Ink-Jet Enabling
	public int bPrintEnable;

	// WORKING MODE
	// One Document feeding option
	public int bOneDoc;

	// Feeding options 
	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
C#
//------------------------------------------------------------------------------------------------------------------------------------------------

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
C#
//------------------------------------------------------------------------------------------------------------------------------------------------
private _DeviceParameters Parameters()
{
	_DeviceParameters m_Parameters = new _DeviceParameters();
	// MICR
	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;

	// IMAGE
	_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;	// Only single Page
	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);

	// PRINTER
	m_Parameters.bPrintEnable = 0;

	// FEEDING OPTIONS
	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];
	// Snippet Enabling
	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.
C#
//------------------------------------------------------------------------------------------------------------------------------------------------

SetDeviceParameters( DeviceID, ToIntPtr < _DeviceParameters > (Parameters()) )

------------------------------------------------------------------------------------------------------------------------------------------------\\
If someone can help me, thanks.
Posted
Updated 22-Jun-22 20:36pm
v2
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900