tags:

views:

335

answers:

2

Can anybody translate the following code to C++? Is this possible at all or are there vital information missing?

Dim Laser As Object
Sub EnableLaser
    ‘ Create a laser object if it hasn’t been done yet
    If Laser Is Nothing Then
       Set Laser = CreateObject("NWLaserXControl.NWLaserX")
    End If
    If Laser.Initialize Then
       Laser.RepRate = 10 ‘ set the rep rate to 10Hz
       Laser.LaserEnabled = True ‘ turn on laser power supply, get it ready
    End If
End Sub
+1  A: 
// if (CoInitialize(0) == S_OK)
{
    CComPtr<INWLaserX> pMyPtr = NULL;

    CLSID clsid = IID_NULL;
    CLSIDFromProgID("NWLaserXControl.NWLaserX");

    if (pMyPtr.CoCreateInstance(clsid) == S_OK)
    {
         pMyPtr->put_RepRate(10);
         pMyPtr->put_LaserEnabled(TRUE);
    }

   // CoUnInitialize();
}
Vinay
@Vinay: +1, but it should be mentioned that you probably don't need to call CoInitialize every time. Once on app startup, and once on teardown (if you have one apartment) is fine.
casperOne
Yes you are right, updated accordingly
Vinay
You can get the CLSID using CLSIDFromProgId.
Ismael
It'll be cleaner to replace "TRUE" with "VARIANT_TRUE".
sharptooth
+1  A: 

There are great tool for that, and it is free : Converts Visual Basic Automation Code to Visual C++, Article-ID: Q216388
But it translate for plain Win32.

lsalamon