Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
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);				
			}
		}
     }
}


C++
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);
	       }
         }
}


C++
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;
}




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:
C++
extern "C" DLL_API  void ParaSetting(HWND _hwnd);
void ParaSetting(HWND _hwnd)
{
	CreateDialog(hInstance, MAKEINTRESOURCE(IDD_2001), _hwnd, (DLGPROC)MainuDlgProc1);
}


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?
Posted
Updated 18-Sep-15 17:17pm
v6
Comments
Sergey Alexandrovich Kryukov 19-Sep-15 2:59am    
It does not seem to make any sense. What makes you thinking that sending message needs a loop?... and so on.
—SA
Richard MacCutchan 19-Sep-15 4:09am    
It would be quicker just to rewrite the dialog in C#.
CPallini 19-Sep-15 8:34am    
Indeed, possibly quicker, much better for sure.

1 solution

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.
 
Share this answer
 

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