chriskr7
July 28th, 2008, 05:16 AM
Dear Gurus,
I'm having a problem with making sample program for my DLL.
My DLL is win32 standard DLL coded in VC 6.0 however I have to offer
sample source for C# how to use it. And.. I am not any good C# programmer just know basic grammers..
Here is the question.
My DLL
/*
* Exported Function
*/
extern "C" INNOCCARDCTRL_API bool FindCardNo(_card_info*);
extern "C" INNOCCARDCTRL_API pPrintProcessInfo GetMatchDocu(char*);
extern "C" INNOCCARDCTRL_API void registerCard(char*, char*);
extern "C" INNOCCARDCTRL_API void releasePrintProcess(pPrintProcessInfo pPrint);
extern "C" INNOCCARDCTRL_API void setPortNumber(unsigned int port_num);
extern "C" INNOCCARDCTRL_API void setPrintName(char* newName);
extern "C" INNOCCARDCTRL_API bool processPrint(unsigned int jobID);
/*
* Card Information
*/
typedef struct __card_info
{
char card_no[17];
char valid_year_mon[5];
} _card_info;
/*
* Document Information
*/
typedef struct __docu_info
{
char document_name[MAX_PATH];
int job_id;
} _docu_info;
typedef _docu_info* pDocuInfo;
/*
* Print Process Info Structure
*/
typedef struct __print_process_info
{
int size;
BYTE response_code;
pDocuInfo pDocumentInfo;
} _print_process_info;
typedef _print_process_info* pPrintProcessInfo;
my C# code
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _card_info
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
public string card_no;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string valid_year_mon;
public _card_info(int i)
{
card_no = "0";
valid_year_mon = "0";
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _docu_info
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string document_name;
public int job_id;
public _docu_info(int i)
{
document_name = "0";
job_id = 0;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _print_process_info
{
public int size;
public ushort response_code;
public IntPtr pDocumentInfo;
public _print_process_info(int i)
{
size = 0;
response_code = 0;
pDocumentInfo = IntPtr.Zero;
}
}
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern bool FindCardNo(ref _card_info card_info);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr GetMatchDocu(string s);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void registerCard(string x, string y);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void releasePrintProcess(IntPtr print);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void setPortNumber(uint port_num);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void setPrintName(string newName);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int processPrint(uint job);
private _docu_info[] docuInfo;
private void verificationButton_Click(object sender, EventArgs e)
{
_card_info card_info = new _card_info();
if (FindCardNo(ref card_info) != true)
{
MessageBox.Show("Grrr.");
return;
}
IntPtr printPtr = GetMatchDocu(card_info.card_no);
_print_process_info printProcessInfo = (_print_process_info)Marshal.PtrToStructure(printPtr, typeof(_print_process_info));
docuInfo = new _docu_info [printProcessInfo.size];
Marshal.Copy(printProcessInfo.pDocumentInfo, docuInfo, printProcessInfo.size, 0);
for(int i = 0; i < printProcessInfo.size; i++)
checkedListBox1.Items.Add(docuInfo[i].document_name);
}
I am having trouble to map pDocumentInfo to docuInfo.
It's IntPtr in structure _print_process_info. and its size is determined
in DLL and allocated in DLL dynamically.
I thought Marshal.Copy should have worked but it didn't.
Please show me how to do it right or any advice will be appeciated.
Thanks in advance.
I'm having a problem with making sample program for my DLL.
My DLL is win32 standard DLL coded in VC 6.0 however I have to offer
sample source for C# how to use it. And.. I am not any good C# programmer just know basic grammers..
Here is the question.
My DLL
/*
* Exported Function
*/
extern "C" INNOCCARDCTRL_API bool FindCardNo(_card_info*);
extern "C" INNOCCARDCTRL_API pPrintProcessInfo GetMatchDocu(char*);
extern "C" INNOCCARDCTRL_API void registerCard(char*, char*);
extern "C" INNOCCARDCTRL_API void releasePrintProcess(pPrintProcessInfo pPrint);
extern "C" INNOCCARDCTRL_API void setPortNumber(unsigned int port_num);
extern "C" INNOCCARDCTRL_API void setPrintName(char* newName);
extern "C" INNOCCARDCTRL_API bool processPrint(unsigned int jobID);
/*
* Card Information
*/
typedef struct __card_info
{
char card_no[17];
char valid_year_mon[5];
} _card_info;
/*
* Document Information
*/
typedef struct __docu_info
{
char document_name[MAX_PATH];
int job_id;
} _docu_info;
typedef _docu_info* pDocuInfo;
/*
* Print Process Info Structure
*/
typedef struct __print_process_info
{
int size;
BYTE response_code;
pDocuInfo pDocumentInfo;
} _print_process_info;
typedef _print_process_info* pPrintProcessInfo;
my C# code
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _card_info
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
public string card_no;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string valid_year_mon;
public _card_info(int i)
{
card_no = "0";
valid_year_mon = "0";
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _docu_info
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string document_name;
public int job_id;
public _docu_info(int i)
{
document_name = "0";
job_id = 0;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _print_process_info
{
public int size;
public ushort response_code;
public IntPtr pDocumentInfo;
public _print_process_info(int i)
{
size = 0;
response_code = 0;
pDocumentInfo = IntPtr.Zero;
}
}
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern bool FindCardNo(ref _card_info card_info);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr GetMatchDocu(string s);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void registerCard(string x, string y);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void releasePrintProcess(IntPtr print);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void setPortNumber(uint port_num);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern void setPrintName(string newName);
[DllImport("InnoCCardCtrl.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int processPrint(uint job);
private _docu_info[] docuInfo;
private void verificationButton_Click(object sender, EventArgs e)
{
_card_info card_info = new _card_info();
if (FindCardNo(ref card_info) != true)
{
MessageBox.Show("Grrr.");
return;
}
IntPtr printPtr = GetMatchDocu(card_info.card_no);
_print_process_info printProcessInfo = (_print_process_info)Marshal.PtrToStructure(printPtr, typeof(_print_process_info));
docuInfo = new _docu_info [printProcessInfo.size];
Marshal.Copy(printProcessInfo.pDocumentInfo, docuInfo, printProcessInfo.size, 0);
for(int i = 0; i < printProcessInfo.size; i++)
checkedListBox1.Items.Add(docuInfo[i].document_name);
}
I am having trouble to map pDocumentInfo to docuInfo.
It's IntPtr in structure _print_process_info. and its size is determined
in DLL and allocated in DLL dynamically.
I thought Marshal.Copy should have worked but it didn't.
Please show me how to do it right or any advice will be appeciated.
Thanks in advance.