LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using C++ Code In LabWindows

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.Smiley Wink

0 Kudos
Message 1 of 7
(9,122 Views)

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:

 

  1. Put your C++ code into a separate DLL (compiled using VC++, say) that is callable from CVI code (note that the callable functions must therefore have an extern "C" interface).
  2. Convert your CVI project to a VC++ project, and perform your development wholly within the VC++ environment (in which C++ and C code may be mixed).
  3. Translate your C++ code into C code that can be used in CVI.

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!

--
Martin
Certified CVI Developer
Message 2 of 7
(9,110 Views)

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

0 Kudos
Message 3 of 7
(9,058 Views)

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

0 Kudos
Message 4 of 7
(9,055 Views)

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().

0 Kudos
Message 5 of 7
(9,016 Views)
I tried method (2) for my application, but it seems the environment hadn't been changed, i.e. the compiler is still C, not a C++.  I used visual C++ 2005->Labwindows/CVI conversion. When my project was sucessfully converted, I tried to add some C++ code and compiled, there are some errors to say the compiler is C type. Do I need to change all my CVI code to C++ type code?
0 Kudos
Message 6 of 7
(8,594 Views)

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

0 Kudos
Message 7 of 7
(8,575 Views)