I'm setting a program to create a COM connection from an ocx file.
I'm using visual studio 2019 with debug x86. It correctly generate the .tlh and .tli files.
My failed tests:
- Use visualtudio 2017 and 2019
Use
CoCreateInstance:IMachine* machine = NULL; // this gives the same error: HRESULT hr2 = CoCreateInstance(CLSID_Machine, NULL, CLSCTX_INPROC_SERVER, IID_IMachine, reinterpret_cast<LPVOID*>(&machine));Replace
CLSID_Machineby the_uuidof(IMachine)Replace
CLSID_Machineby theLPCWSTR clsidString
This is a part of my code:
#import "lib.ocx" no_namespace , named_guids
HRESULT hr1 = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
try {
if (SUCCEEDED(hr1))
{
IMachinePtr machineCUPtr;
HRESULT hr2 = machineCUPtr.CreateInstance(CLSID_Machine, NULL,
CLSCTX_INPROC_SERVER); //Failure
if (FAILED(hr2))
{
std::cout << "Fail:" << hr2 << std::endl;
}
else {
std::cout << "Success: " << hr2 << std::endl;
}
}
else {
std::cout << "Fail CoInit: " << std::endl;
}
}
catch (_com_error& e)
{
std::cout << "COM ERROR catched " << std::endl;
std::cout << "Code = %08lx\n" << e.Error() << std::endl;
std::cout << "Meaning = %s\n" << e.ErrorMessage() << std::endl;
std::cout << "Source = %s\n" << (LPCSTR)e.Source() << std::endl;
std::cout << "Description = %s\n" << (LPCSTR)e.Description() << std::endl;
}
CoUninitialize();
}
I still have this error:
Exception raised to 0x00B20768 in myApp.exe : 0xC000000005 : Access violation during execution at location 0x00B20768.
Edit:
'''
#include <iostream>
#import "libcom.ocx"
using namespace LIBCOM;
int main()
{
CoInitialize(NULL);
IMachinePtr machineCUPtr(__uuidof(Machine)); //Same error
machineCUPtr->MyMethod(parameters of the method);
CoUninitialize();
return 0;
}
''''