05-27-2009 03:26 AM
Hi,
We must integrate C++ code in LabWindows. We don't know how to pass this C++ code to C code. We think we have to use different #includes but we don't know which. We have the following code:
BOOL CALLBACK OurEnumWindowsProcSL2
(HWND hwnd,LPARAM lParam)
{
CString cStr;
CWnd* pWnd = CWnd::FromHandle(hwnd);
__hWnd = NULL;
if (pWnd == NULL) return TRUE;
pWnd->GetWindowText(cStr);
if (cStr == "CCDAcquire")
{
__hWnd = hwnd;
return FALSE;
};
return TRUE;
};
Thanks for your help.
05-27-2009 05:18 AM
The simple answer is that you can't. LabWindows/CVI only has a C compiler, not a C++ compiler. Therefore, you need to find a workaround. Perhaps the most common workarounds are:
If you don't have much C++ code, then (3) might be your best bet. In the case of your EnumWindows() callback, one translation might be:
// Don't forget to install the Windows SDK and // #include <windows.h> to make this work BOOL CALLBACK OurEnumWindowsProcSL2 (HWND hwnd,LPARAM lParam) { char textBuffer[80]; // working buffer of nominal length __hWnd = NULL; if (0 >= GetWindowText(hwnd, textBuffer, 80)) return TRUE; if (0 == strcmp(textBuffer, "CCDAcquire")) { __hWnd = hwnd; return FALSE; } return TRUE; }
Hope that helps!
05-28-2009 03:13 AM
Hi pals,
thanks for your help, I think it would be helpful. We have another part of the code to translate to C from C++. We are trying the DLL option but if you can help us translating C++ code to C we will be very grateful. This is our code:
void CTestTriggerSL2Dlg::OnButtonTriggerSL2()
{
LRESULT res;
EnumWindows (OurEnumWindowsProcSL2,0);
if (__hWnd != NULL)
{
res=::SendMessage (__hWnd, WM_TRIGGER_ACQUISITION,0,0); // WM_TRIGGER_ACQUISITION=4567=START ACQUISITION
else printf("Can´t start acquisition");
do
{
Sleep(1000);
res=::SendMessage(__hWnd,WM_QUERY_ACQUISITION,0,0); // WM_QUERY_ACQUISITION=4568=TEST ACQUISITION
}
while (res<0);
printf("%d cycles measured",res);
};
};
Thanks a lot
05-28-2009 03:49 AM
hi msaxon & company,
We have done a traslation of this last code but we don´t know if it is correct. If you can check it, thank you.
int CVICALLBACK Initialize (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
// void CTestTriggerSL2Dlg::OnButtonTriggerSL2()
{
LRESULT res;
EnumWindows (OurEnumWindowsProcSL2,0);
if (__hWnd != NULL)
res=SendMessage (__hWnd, WM_TRIGGER_ACQUISITION,0,0);
else
printf("Can´t start acquisition");
do
{
Sleep(1000);
res=SendMessage(__hWnd,WM_QUERY_ACQUISITION,0,0);
}
while (res<0);
printf("%d cycles measured",res);
};
break;
case EVENT_RIGHT_CLICK:
break;
}
return 0;
}
05-29-2009 09:02 AM
your translation is perfect, it is the absolute C translation for this snippet of C++.
however, the original C++ code is far from perfect:
1. variable names beginning with '_' are reserved for thestandard C or C++ library, so __hWnd is not a good variable name. it will still compile, but such names should be avoided.
2. __hWnd is a GLOBAL variable, and globals are BAAAAAD ! if, by any chance, someone manages to hit your button twice at the same time, the enumeration proc will be executed 2 times simultaneously and the content of the __hWnd variable may be undefined. the second parameter to EnumWindows() is made specifically to transfer data between the enumeration function and the calling code. __hWnd should be declared local to your Initialize() function and its address passed as the second parameter to EnumWindows().
09-14-2009 01:43 PM
09-15-2009 03:10 AM
Are you trying to use the Visual Studio compiler to compile C++ code? If so then you are probably aware that its default operation is to compile *.c files as C source code, and *.cpp files as C++ code. Maybe all you need to do is change your source code filename accordingly.
JR