如何通过c sharp和win32库? [英] how to commucation c sharp and win32 library?
问题描述
我的目的是将win32应用程序更改为win32 libray,并使用c#应用程序调用win32 libray。
但是会出现一些问题。
win32应用程序的问题写如下:
my purpose is to change win32 application to win32 libray,and use c# application to invoke win32 libray.
but there some questions that occurs.
the win32 application's problem write as below:
int WINAPI WinMain(HINSTANCE hInstance, // current form instance
HINSTANCE hPrevInstance, // pre form instance
LPSTR lpCmdLine, // parameters command
int nCmdShow) // show the form states
{
step 1: create main form use api function.
step 2: create while to catch windows message
while(!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // is there have message to wait?
{
if (msg.message==WM_QUIT)
done=TRUE; // yes, receive the quit message
else // no
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
LRESULT CALLBACK WndProc(HWND hWnd, // current form's instance
UINT uMsg, // windows message
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg) //check message
{
if(LOWORD(wParam)==1001)
{
CreateDialog(hInstanceTemp, MAKEINTRESOURCE(IDD_2001), hWnd, (DLGPROC)MainuDlgProc1);
EnableWindow(GetDlgItem(hWnd,1001),FALSE);
}
}
}
BOOL WINAPI MainuDlgProc1(HWND u_Dlg, UINT u_Msg, WPARAM w_Param, LPARAM l_Param)
{
switch(u_Msg)
{
case WM_INITDIALOG:
if(Dlg01==1)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON1),FALSE);
else if(Dlg01==2)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON2),FALSE);
else if(Dlg01==3)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON3),FALSE);
else if(Dlg01==4)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON4),FALSE);
else if(Dlg01==5)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON5),FALSE);
else if(Dlg01==6)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON6),FALSE);
else if(Dlg01==7)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON7),FALSE);
else if(Dlg01==8)
EnableWindow(GetDlgItem(u_Dlg,IDC_BUTTON8),FALSE);
char Dlg_name[30];
sprintf(Dlg_name,"%d",fishq[0].q10);
SetDlgItemText(u_Dlg,IDC_EDIT1,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q11);
SetDlgItemText(u_Dlg,IDC_EDIT2,Dlg_name);
sprintf(Dlg_name,"%d",fishq[0].q19);
SetDlgItemText(u_Dlg,IDC_EDIT3,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q12);
SetDlgItemText(u_Dlg,IDC_EDIT4,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q13*10000.0);
SetDlgItemText(u_Dlg,IDC_EDIT5,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q15*10000.0);
SetDlgItemText(u_Dlg,IDC_EDIT7,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q16);
SetDlgItemText(u_Dlg,IDC_EDIT8,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q17);
SetDlgItemText(u_Dlg,IDC_EDIT9,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q20);
SetDlgItemText(u_Dlg,IDC_EDIT10,Dlg_name);
sprintf(Dlg_name,"%g",fishq[0].q22);
SetDlgItemText(u_Dlg,IDC_EDIT11,Dlg_name);
sprintf(Dlg_name,"%d",fishm2.tt);
SetDlgItemText(u_Dlg,IDC_EDIT12,Dlg_name);
sprintf(Dlg_name,"%g",fishq[1].q52);
SetDlgItemText(u_Dlg,IDC_EDIT13,Dlg_name);
sprintf(Dlg_name,"%g",fishq[2].q52);
SetDlgItemText(u_Dlg,IDC_EDIT14,Dlg_name);
sprintf(Dlg_name,"%g",fishq[3].q52);
SetDlgItemText(u_Dlg,IDC_EDIT15,Dlg_name);
sprintf(Dlg_name,"%g",fishq[4].q52);
SetDlgItemText(u_Dlg,IDC_EDIT16,Dlg_name);
sprintf(Dlg_name,"%g",fishq[5].q52);
SetDlgItemText(u_Dlg,IDC_EDIT17,Dlg_name);
sprintf(Dlg_name,"%g",fishq[6].q52);
SetDlgItemText(u_Dlg,IDC_EDIT18,Dlg_name);
sprintf(Dlg_name,"%g",fishq[7].q52);
SetDlgItemText(u_Dlg,IDC_EDIT19,Dlg_name);
sprintf(Dlg_name,"%i",fish_o2);
SetDlgItemText(u_Dlg,IDC_EDIT20,Dlg_name);
if(fishq[0].q1 & (1UL<< 4)) CheckRadioButton(u_Dlg,IDC_RADIO5,IDC_RADIO6,IDC_RADIO5);
else CheckRadioButton(u_Dlg,IDC_RADIO5,IDC_RADIO6,IDC_RADIO6);
if(fishq[0].q1 & (1UL<< 3)) CheckRadioButton(u_Dlg,IDC_RADIO7,IDC_RADIO8,IDC_RADIO7);
else CheckRadioButton(u_Dlg,IDC_RADIO7,IDC_RADIO8,IDC_RADIO8);
if(fishq[0].q1 & (1UL<< 2)) CheckRadioButton(u_Dlg,IDC_RADIO9,IDC_RADIO10,IDC_RADIO9);
else CheckRadioButton(u_Dlg,IDC_RADIO9,IDC_RADIO10,IDC_RADIO10);
if(fishq[0].q1 & (1UL<< 1)) CheckRadioButton(u_Dlg,IDC_RADIO11,IDC_RADIO12,IDC_RADIO11);
else CheckRadioButton(u_Dlg,IDC_RADIO11,IDC_RADIO12,IDC_RADIO12);
if(fishq[0].q1 & (1UL<< 0)) CheckRadioButton(u_Dlg,IDC_RADIO13,IDC_RADIO14,IDC_RADIO13);
else CheckRadioButton(u_Dlg,IDC_RADIO13,IDC_RADIO14,IDC_RADIO14);
sprintf(Dlg_name,"%c",fishq[Dlg01].q1);
SetDlgItemText(u_Dlg,IDC_EDIT30,Dlg_name);
sprintf(Dlg_name,"%d",fishq[Dlg01].q2);
SetDlgItemText(u_Dlg,IDC_EDIT31,Dlg_name);
sprintf(Dlg_name,"%d",fishq[Dlg01].q3);
SetDlgItemText(u_Dlg,IDC_EDIT32,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q4);
SetDlgItemText(u_Dlg,IDC_EDIT33,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q5);
SetDlgItemText(u_Dlg,IDC_EDIT34,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q6);
SetDlgItemText(u_Dlg,IDC_EDIT35,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q7);
SetDlgItemText(u_Dlg,IDC_EDIT36,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q8);
SetDlgItemText(u_Dlg,IDC_EDIT37,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q9);
SetDlgItemText(u_Dlg,IDC_EDIT38,Dlg_name);
sprintf(Dlg_name,"%d",fishq[Dlg01].q10);
SetDlgItemText(u_Dlg,IDC_EDIT39,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q11);
SetDlgItemText(u_Dlg,IDC_EDIT40,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q12);
SetDlgItemText(u_Dlg,IDC_EDIT41,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q13*10000.0);
SetDlgItemText(u_Dlg,IDC_EDIT42,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q14);
SetDlgItemText(u_Dlg,IDC_EDIT43,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q15);
SetDlgItemText(u_Dlg,IDC_EDIT44,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q16);
SetDlgItemText(u_Dlg,IDC_EDIT45,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q17);
SetDlgItemText(u_Dlg,IDC_EDIT46,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q18);
SetDlgItemText(u_Dlg,IDC_EDIT47,Dlg_name);
sprintf(Dlg_name,"%d",fishq[Dlg01].q19);
SetDlgItemText(u_Dlg,IDC_EDIT48,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q20);
SetDlgItemText(u_Dlg,IDC_EDIT49,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q21);
SetDlgItemText(u_Dlg,IDC_EDIT50,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q22);
SetDlgItemText(u_Dlg,IDC_EDIT51,Dlg_name);
sprintf(Dlg_name,"%g",fishq[Dlg01].q23);
SetDlgItemText(u_Dlg,IDC_EDIT52,Dlg_name);
break;
case WM_COMMAND:
if(LOWORD(w_Param)==IDOK || LOWORD(w_Param)==IDC_BUTTON1 || LOWORD(w_Param)==IDC_BUTTON2 || LOWORD(w_Param)==IDC_BUTTON3 || LOWORD(w_Param)
==IDC_BUTTON4 || LOWORD(w_Param)==IDC_BUTTON5 || LOWORD(w_Param)==IDC_BUTTON6 || LOWORD(w_Param)==IDC_BUTTON7 || LOWORD(w_Param)==IDC_BUTTON8)
{
//......
}
else if( LOWORD(w_Param) == IDCANCEL)//取消
{
EnableWindow(GetDlgItem(hWnd,1001),TRUE);
EndDialog(u_Dlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
EnableWindow(GetDlgItem(hWnd,1001),TRUE);
EndDialog(u_Dlg, 0);
break;
case WM_QUIT://退出对话框
EnableWindow(GetDlgItem(hWnd,1001),TRUE);
EndDialog(u_Dlg, 0);
break;
}
return FALSE;
}
我的问题是:
如果我将win32应用程序更改为库,并使用c#form调用libray。
first:删除步骤1,然后删除步骤2,即删除win32表单。
秒:将csharp.h添加到win32,并定义如下函数:
my question is:
if i change the win32 application to library,and use c# form to invoke libray.
first: remove the step 1,and remove the step 2,that is remove the win32 form.
second:add csharp.h to win32,and define function as below:
extern "C" DLL_API void ParaSetting(HWND _hwnd);
void ParaSetting(HWND _hwnd)
{
CreateDialog(hInstance, MAKEINTRESOURCE(IDD_2001), _hwnd, (DLGPROC)MainuDlgProc1);
}
所以我可以使用dllimport来调用win32 ParaSetting函数。
但是问题是:当我调用ParaSetting函数时,它无法创建对话框表单,因为win32库不包含while循环发送消息。
我怎样才能解决这个问题并实现相同的功能作为win32应用程序?
so i can use dllimport to invoke the win32 ParaSetting function.
but the question is : when i invoke ParaSetting function,it can't create the dialog form, because the win32 library not contains the while loop to send message.
how can i solve this quesion and achive the same function as win32 application?
推荐答案
我必须让理查德和谢尔盖认为你的代码非常不清楚且难以理解。最好用C#重写所有内容。它使调试更容易。
对于C ++和C#之间的交互,您可以查看我的文章呼叫所有电台和附加的代码。
I must commit Richard and Sergey that your code is very unclear and hard to understand. And it would better to rewrite all in C#. It makes also debugging easier.
For interaction between C++ and C# you can take a look on my article Calling All Stations and the attached code.
这篇关于如何通过c sharp和win32库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!