question

SaiPrashanthJosyula-8072 avatar image
0 Votes"
SaiPrashanthJosyula-8072 asked RLWA32-6355 edited

_com_error at memory location 0x00BCF818 due to HRESULT initialization

I am very new to C++ programming and I have to run the below code in order to achieve communication via a COM port to an external device. Most of the below code is regarding what to do once the communication is established ( I assume). I am unable to get the desired output out of the code due to a series of exceptions starting with the following.

Exception thrown by comip.h
Unhandled exception at 0x7597A6F2 in UnmanagedC++Example.exe: Microsoft C++ exception: _com_error at memory location 0x00DBFE48.

Starting part of the code that is actually causing all these errors is :

 #include "stdafx.h"
 #include "tchar.h"
 #include "windows.h"
 #include "comip.h"
 #import "D:\PhD_Work\NanoPro\CommandsPD4I.tlb" raw_interfaces_only
 using namespace CommandsPD4I;
    
 int _tmain(int argc, _TCHAR* argv[])
 {
     **// Initialize COM.
     HRESULT hr = CoInitialize(NULL);**
 //And the code goes on....

Kindly give me your inputs. I am working in Visual Studio 2019

Cheers
Prashanth

windows-apic++
· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm sorry I can't reproduce your problem with this code snippet, could you please show a minimal, reproducible sample without private information?

0 Votes 0 ·

Code Snippet:

     HRESULT hr = CoInitialize(NULL);
    
        
    
     // Create the interface pointers.
     IComMotorCommandsPtr motor1(__uuidof(ComMotorCommands));
     IComMotorCommandsPtr motor2(__uuidof(ComMotorCommands));
     printf("ssss");
     long lResult = 0;
     VARIANT_BOOL bResult = false;
    
     BSTR port1 = SysAllocString(L"COM3");
     BSTR port2 = SysAllocString(L"COM3");
        
    
     // Set comm settings for motor 1
     motor1->put_SelectedPort(port1);
     motor1->put_Baudrate(115200);
 // Code goes on and ends at 
       
     CoUninitialize();
     return 0;
 }

is this useful for you to verify what error is popping up.

Cheers
Prashanth

0 Votes 0 ·
DavidLowndes-6766 avatar image DavidLowndes-6766 SaiPrashanthJosyula-8072 ·

Anyone would need to have installed the 3'rd party component, so it's not much use.

0 Votes 0 ·
SongZhu-MSFT avatar image SongZhu-MSFT SaiPrashanthJosyula-8072 ·

This code snippet cannot help reproduce the problem, perhaps the problem is related to your third-party component.

0 Votes 0 ·

Serial communication through a COM port is not normally related to MS "COM" (Component Object Model), so I don't know why you're showing code calling CoInitialize.

0 Votes 0 ·

This was the code given by the company whose device I want to control. But I am unable to get the desired output due to the exception.
Cheers
Prashanth

0 Votes 0 ·
DavidLowndes-6766 avatar image DavidLowndes-6766 SaiPrashanthJosyula-8072 ·

Aha, so they've supplied you with a COM object to talk to their device?
In that case I can only suggest that you get back to them.

0 Votes 0 ·
Show more comments

You may be able to get some useful information about the exception by using the following -

         try
         {
             //Insert your code here
         }
         catch (_com_error& e)
         {
             _tprintf_s(_T("Com error was \"%s\", HRESULT 0x%X\n"), e.ErrorMessage(), e.Error());
         }
     }


0 Votes 0 ·

0 Answers