COM - using BSTR



  • Hi

    1. Situation
      ============
      My task is to write a program in VC++ which makes a windows program communicate to a sensor (MTi). There is a fully functional command line program from the producer of the sensor. Now i want to change severel settings (using specific functions) of the sensor like sample frequency and so on. The main function makes a Motion Tracker Object (written in c) to realize the communication through a virtual COM Port.

    2. Problem
      ==========
      I want to change the sample frequency of the data exchange between the program and the sensor (default it is 100Hz which is to fast for my application):

    <code>
    int nSFreq=25; //Sample Frequency in Hz
    int nUSFreq=0; //Used sample Frequency
    //Set new sample Frequency where pMT is the created Motion Tracker Object
    pMT->MT_SetMotionTrackerSampleFrequency(nSFreq);
    //Show the Sample Frequency for checkup
    MT_GetMotionTrackerSampleFrequency(&nUSFreq);
    //Show it on command Line
    printf("Your sample Frequency %d", nUSFreq);
    </code>

    --> the command line shows 25 as expected, but the sensor samples still with 100Hz. Now i read that i have to safe the settings on the sensor using the sensor ID which is a BSTR. The ID i can get from this function:

    <code>
    //get the device ID
    virtual HRESULT STDMETHODCALLTYPE MT_QueryMotionTrackerB(
    /* [out] */ BSTR __RPC_FAR *bstrDeviceID) = 0;
    //safe the new settings trough this function
    virtual HRESULT STDMETHODCALLTYPE MT_SaveToMTS(
    BSTR bstrDeviceID) = 0;
    </code>

    --> now, what i have done in my program:

    <code>
    BSTR nDId;
    //get the Device ID
    pMT->MT_QueryMotionTrackerB(&nDId);
    //safe settings
    pMT->MT_SaveToMTS(nDId);
    </code>

    -->The program compiles very nice but when i run the .exe i got this damn error message all the time:
    "An exception 'System.NullRefereceException' has occured in MTi.cmd1.exe"
    (-->The error is only caused when i use the BSTR function shown above)

    I know the problem is this BSTR but i cant help myself. Maybe someone can tell me what i need to do.
    1000Thx

    chris


  • Mod

    First: This is a german fourm.
    Second: A BSTR is a special wchar_t string. When you want to use it you need to allocate one with SysAllocString. Also you can use CComBSTR.
    Third: You haven't shown all the interface functions you want to use. Also is MT_QueryMotionTrackerB a standard COM interface function. In this case it depends is MT_QueryMotionTrackerB is declared as in/out or only as out. If it is declared as in/out you have to pass a valid BSTR into the function. If declared only as out I wonder about the error.



  • Zu 1) Dann schreibe ich Deutsch, wenn das Englisch nicht geläufig ist.

    Zu 2) Glaube ich nicht, ich möchte den BSTR ja nicht weiter verwenden, sondern direkt wieder an das COM Objekt übergeben.

    Zu 3) Die Funktionen sind wie folgt deklariert. Ich nehme an, es handelt sich um einen reinen out, wie es im Kommentar vermerkt ist. Ich habe das COM Objekt nicht selbst programmiert.

    <code>
    //get the device ID
    virtual HRESULT STDMETHODCALLTYPE MT_QueryMotionTrackerB(
    /* [out] */ BSTR __RPC_FAR *bstrDeviceID) = 0;
    //safe the new settings trough this function
    virtual HRESULT STDMETHODCALLTYPE MT_SaveToMTS(
    BSTR bstrDeviceID) = 0;
    </code>

    Zum letzten Satz) Ich kriege tatsächlich keine Error Meldung mehr. Was aber noch viel schlimmer ist, weil er etwas compiled aber nicht korrekt ausführt.

    Ich Versuchs weiter


  • Mod

    chris001z schrieb:

    Zu 1) Dann schreibe ich Deutsch, wenn das Englisch nicht geläufig ist.

    Was heißt nicht geläufig. Englische Gruppen kannst Du gerne besuchen und dort dann Deutsch schreiben. Viel Spass beim warten auf Antworten.
    Dies ist eine Deutsche Gruppe, also warum nicht Deutsch schreiben?

    chris001z schrieb:

    Zu 2) Glaube ich nicht, ich möchte den BSTR ja nicht weiter verwenden, sondern direkt wieder an das COM Objekt übergeben.

    Wenn die Funktion orndungsgemäß funktionieren würde und den BSTR als out übergibt, dann müsste dieser auch gefüllt werden.
    Du müsstest ihn auch selbst anschließend entsorgen.

    chris001z schrieb:

    Zu 3) Die Funktionen sind wie folgt deklariert. Ich nehme an, es handelt sich um einen reinen out, wie es im Kommentar vermerkt ist. Ich habe das COM Objekt nicht selbst programmiert.

    <code>
    //get the device ID
    virtual HRESULT STDMETHODCALLTYPE MT_QueryMotionTrackerB(
    /* [out] */ BSTR __RPC_FAR *bstrDeviceID) = 0;
    //safe the new settings trough this function
    virtual HRESULT STDMETHODCALLTYPE MT_SaveToMTS(
    BSTR bstrDeviceID) = 0;
    </code>

    In diesem Fall müsstest Du nur einen Zeiger auf einen BSTR übergeben. Das wars

    chris001z schrieb:

    BSTR myStr=NULL;
    pInterface->MT_QueryMotionTrackerB(&myStr);

    Der Init mit NULL wäre nicht nötig, aber ist manchmal angebracht, wenn der COM Programmierer out und in/out verwechselt hat.

    Anschließend musst Du das gute Stück freigeben. Das würde sich erübrigen bei Verwendung eines CComBSTR!

    chris001z schrieb:

    Zum letzten Satz) Ich kriege tatsächlich keine Error Meldung mehr. Was aber noch viel schlimmer ist, weil er etwas compiled aber nicht korrekt ausführt.

    Tja... Entwickler kontaktieren...
    :xmas2:
    Ich Versuchs weiter


Anmelden zum Antworten