weicheun
asked on
How to transalate this eVb code to evc code
I am trying to translate the EVB code below to Embeded Visual C++:
Dim sync As MSync.sync
Set sync = CreateObject("MSync.Sync")
Dim oRAS As CEUTILSLibCtl.RASConnectio n
Set oRAS = CreateObject("CEUtils.RASC onnection" )
PDAConn = oRAS.Connection
msync is an exe file and ceutil is a dll file. How can i accomplish the same thing with evc++
how do i declare and use the dll in evc?
Dim sync As MSync.sync
Set sync = CreateObject("MSync.Sync")
Dim oRAS As CEUTILSLibCtl.RASConnectio
Set oRAS = CreateObject("CEUtils.RASC
PDAConn = oRAS.Connection
msync is an exe file and ceutil is a dll file. How can i accomplish the same thing with evc++
how do i declare and use the dll in evc?
You are using COM objects. In C++, you will need to call CoCreateInstance().
ASKER
My program will start a third party program(MSYNC). I want my program to press/execute the "SYNC" button in the program. How do I do that without instucting the mouse to move to the cordinate of sync button and click?
in Visual Basic its just MSYNC.sync but i can't figure out how to do it in C++
in Visual Basic its just MSYNC.sync but i can't figure out how to do it in C++
As I already told you, you need to create the COM object and call the methods on its interface.
ASKER
can u show me how to write the code?
There is nothing special. Just like any COM calls,
HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
IYourInterface *pInterface = NULL;
hr = ::CoCreateInstance(CLSID_Y OURCLSID, NULL, CLSCTX_INPROC_SERVER, IID_YOURINTERFACE, (void **)&pInterface);
if (SUCCEEDED(hr))
{
pInterface->TheMethod();
pInterface->Release();
}
::CoUninitialize();
HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
IYourInterface *pInterface = NULL;
hr = ::CoCreateInstance(CLSID_Y
if (SUCCEEDED(hr))
{
pInterface->TheMethod();
pInterface->Release();
}
::CoUninitialize();
ASKER
what should i put into the fifth parameter?
I put {16F4A261-A648-11D5-A2C5-0 0C04F81C07 3} in the first parameter.
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: <could not determine filename>
[
uuid(16F4A254-A648-11D5-A2 C5-00C04F8 1C073),
version(1.0),
helpstring("mSync 1.0 Type Library")
]
library MSYNC
{
// TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-0 0000000004 6}
importlib("stdole2.tlb");
// Forward declare all types defined in this typelib
dispinterface _ISyncProgressListenerEven ts;
interface ISyncOption;
interface ISync;
interface ISyncProgressListener;
[
uuid(BD359C39-AE10-11D5-A2 C7-00C04F8 1C073),
helpstring("_ISyncProgress ListenerEv ents Interface")
]
dispinterface _ISyncProgressListenerEven ts {
properties:
methods:
[id(0x00000001), helpstring("method progress")]
HRESULT progress(
[in] int progressType,
int param1,
int param2);
};
[
uuid(16F4A261-A648-11D5-A2 C5-00C04F8 1C073),
helpstring("SyncOption Class")
]
coclass SyncOption {
[default] interface ISyncOption;
};
[
odl,
uuid(16F4A260-A648-11D5-A2 C5-00C04F8 1C073),
helpstring("ISyncOption Interface"),
dual,
oleautomation
]
interface ISyncOption : IDispatch {
[id(0x00000001), helpstring("method load")]
HRESULT load();
[id(0x00000002), propget, helpstring("property username")]
HRESULT username([out, retval] BSTR* pVal);
[id(0x00000002), propput, helpstring("property username")]
HRESULT username([in] BSTR pVal);
[id(0x00000003), propget, helpstring("property password")]
HRESULT password([out, retval] BSTR* pVal);
[id(0x00000003), propput, helpstring("property password")]
HRESULT password([in] BSTR pVal);
[id(0x00000004), helpstring("method getBytes")]
HRESULT getBytes([out] BSTR* bytes);
[id(0x00000005), propget, helpstring("property transporType")]
HRESULT transporType([out, retval] BSTR* pVal);
[id(0x00000005), propput, helpstring("property transporType")]
HRESULT transporType([in] BSTR pVal);
[id(0x00000006), propget, helpstring("property transportParam")]
HRESULT transportParam([out, retval] BSTR* pVal);
[id(0x00000006), propput, helpstring("property transportParam")]
HRESULT transportParam([in] BSTR pVal);
[id(0x00000007), helpstring("method save")]
HRESULT save();
};
[
uuid(8718BC04-A658-11D5-A2 C6-00C04F8 1C073),
helpstring("Sync Class")
]
coclass Sync {
[default] interface ISync;
[default, source] dispinterface _ISyncProgressListenerEven ts;
};
[
odl,
uuid(8718BC03-A658-11D5-A2 C6-00C04F8 1C073),
helpstring("ISync Interface"),
dual,
oleautomation
]
interface ISync : IDispatch {
[id(0x00000002), helpstring("method doSync")]
HRESULT doSync();
[id(0x00000003), helpstring("method setOptionObject")]
HRESULT setOptionObject([in] ISyncOption* pISyncOption);
[id(0x00000004), propget, helpstring("property lastErrorCode")]
HRESULT lastErrorCode([out, retval] long* pVal);
[id(0x00000005), propget, helpstring("property lastErrorMessage")]
HRESULT lastErrorMessage([out, retval] BSTR* pVal);
};
[
uuid(BD359C38-AE10-11D5-A2 C7-00C04F8 1C073),
helpstring("SyncProgressLi stener Class")
]
coclass SyncProgressListener {
[default] interface ISyncProgressListener;
[default, source] dispinterface _ISyncProgressListenerEven ts;
};
[
odl,
uuid(BD359C37-AE10-11D5-A2 C7-00C04F8 1C073),
helpstring("ISyncProgressL istener Interface"),
dual,
oleautomation
]
interface ISyncProgressListener : IDispatch {
[id(0x00000001), helpstring("method progress")]
HRESULT progress(
[in] int progressType,
int param1,
int param2);
};
};
I put {16F4A261-A648-11D5-A2C5-0
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: <could not determine filename>
[
uuid(16F4A254-A648-11D5-A2
version(1.0),
helpstring("mSync 1.0 Type Library")
]
library MSYNC
{
// TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-0
importlib("stdole2.tlb");
// Forward declare all types defined in this typelib
dispinterface _ISyncProgressListenerEven
interface ISyncOption;
interface ISync;
interface ISyncProgressListener;
[
uuid(BD359C39-AE10-11D5-A2
helpstring("_ISyncProgress
]
dispinterface _ISyncProgressListenerEven
properties:
methods:
[id(0x00000001), helpstring("method progress")]
HRESULT progress(
[in] int progressType,
int param1,
int param2);
};
[
uuid(16F4A261-A648-11D5-A2
helpstring("SyncOption Class")
]
coclass SyncOption {
[default] interface ISyncOption;
};
[
odl,
uuid(16F4A260-A648-11D5-A2
helpstring("ISyncOption Interface"),
dual,
oleautomation
]
interface ISyncOption : IDispatch {
[id(0x00000001), helpstring("method load")]
HRESULT load();
[id(0x00000002), propget, helpstring("property username")]
HRESULT username([out, retval] BSTR* pVal);
[id(0x00000002), propput, helpstring("property username")]
HRESULT username([in] BSTR pVal);
[id(0x00000003), propget, helpstring("property password")]
HRESULT password([out, retval] BSTR* pVal);
[id(0x00000003), propput, helpstring("property password")]
HRESULT password([in] BSTR pVal);
[id(0x00000004), helpstring("method getBytes")]
HRESULT getBytes([out] BSTR* bytes);
[id(0x00000005), propget, helpstring("property transporType")]
HRESULT transporType([out, retval] BSTR* pVal);
[id(0x00000005), propput, helpstring("property transporType")]
HRESULT transporType([in] BSTR pVal);
[id(0x00000006), propget, helpstring("property transportParam")]
HRESULT transportParam([out, retval] BSTR* pVal);
[id(0x00000006), propput, helpstring("property transportParam")]
HRESULT transportParam([in] BSTR pVal);
[id(0x00000007), helpstring("method save")]
HRESULT save();
};
[
uuid(8718BC04-A658-11D5-A2
helpstring("Sync Class")
]
coclass Sync {
[default] interface ISync;
[default, source] dispinterface _ISyncProgressListenerEven
};
[
odl,
uuid(8718BC03-A658-11D5-A2
helpstring("ISync Interface"),
dual,
oleautomation
]
interface ISync : IDispatch {
[id(0x00000002), helpstring("method doSync")]
HRESULT doSync();
[id(0x00000003), helpstring("method setOptionObject")]
HRESULT setOptionObject([in] ISyncOption* pISyncOption);
[id(0x00000004), propget, helpstring("property lastErrorCode")]
HRESULT lastErrorCode([out, retval] long* pVal);
[id(0x00000005), propget, helpstring("property lastErrorMessage")]
HRESULT lastErrorMessage([out, retval] BSTR* pVal);
};
[
uuid(BD359C38-AE10-11D5-A2
helpstring("SyncProgressLi
]
coclass SyncProgressListener {
[default] interface ISyncProgressListener;
[default, source] dispinterface _ISyncProgressListenerEven
};
[
odl,
uuid(BD359C37-AE10-11D5-A2
helpstring("ISyncProgressL
dual,
oleautomation
]
interface ISyncProgressListener : IDispatch {
[id(0x00000001), helpstring("method progress")]
HRESULT progress(
[in] int progressType,
int param1,
int param2);
};
};
Depending on which interface you need to use, the first parameter is the class ID and the fourth parameter is the interface ID. For example,
[
uuid(8718BC04-A658-11D5-A2 C6-00C04F8 1C073),
helpstring("Sync Class")
]
coclass Sync {
[default] interface ISync;
[default, source] dispinterface _ISyncProgressListenerEven ts;
};
[
odl,
uuid(8718BC03-A658-11D5-A2 C6-00C04F8 1C073),
helpstring("ISync Interface"),
dual,
oleautomation
]
Above, the first uuid is the class ID and the second uuid is the interface ID.
[
uuid(8718BC04-A658-11D5-A2
helpstring("Sync Class")
]
coclass Sync {
[default] interface ISync;
[default, source] dispinterface _ISyncProgressListenerEven
};
[
odl,
uuid(8718BC03-A658-11D5-A2
helpstring("ISync Interface"),
dual,
oleautomation
]
Above, the first uuid is the class ID and the second uuid is the interface ID.
ASKER
what's wrong with my code?
1)REFCLSID pclsid =NULL;
2)REFIID intcid =NULL ;
3)CLSIDFromString(TEXT("87 18BC04-A65 8-11D5-A2C 6-00C04F81 C073"),pcl sid ) ;
4)CLSIDFromString(TEXT("87 18BC03-A65 8-11D5-A2C 6-00C04F81 C073"),int cid) ;
5)hr = ::CoCreateInstance(pclsid, NULL, CLSCTX_INPROC_SERVER, intcid, (void **)&pInterface);
error message:
1)'initializing' : cannot convert from 'const int' to 'const struct _GUID
3)'CLSIDFromString' : cannot convert parameter 2 from 'const struct _GUID' to 'struct _GUID *'
5)'CoCreateInstance' : cannot convert parameter 4 from 'struct _GUID *' to 'const struct _GUID &'
1)REFCLSID pclsid =NULL;
2)REFIID intcid =NULL ;
3)CLSIDFromString(TEXT("87
4)CLSIDFromString(TEXT("87
5)hr = ::CoCreateInstance(pclsid,
error message:
1)'initializing' : cannot convert from 'const int' to 'const struct _GUID
3)'CLSIDFromString' : cannot convert parameter 2 from 'const struct _GUID' to 'struct _GUID *'
5)'CoCreateInstance' : cannot convert parameter 4 from 'struct _GUID *' to 'const struct _GUID &'
CLSID c1;
IID i1;
::CLSIDFromString(L"8718BC 04-A658-11 D5-A2C6-00 C04F81C073 ", &c1);
::CLSIDFromString(L"8718BC 03-A658-11 D5-A2C6-00 C04F81C073 ", &i1);
IID i1;
::CLSIDFromString(L"8718BC
::CLSIDFromString(L"8718BC
ASKER
Thanks, now I got another problem?
I received this error message:
'doSync' : is not a member of 'IDispatch'
Here's my code:
#include <windows.h>
#include <Objbase.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
CLSID c1;
IID i1;
::CLSIDFromString(L"8718BC 04-A658-11 D5-A2C6-00 C04F81C073 ", &c1);
::CLSIDFromString(L"8718BC 03-A658-11 D5-A2C6-00 C04F81C073 ", &i1);
ISync : IDispatch* pInterface = NULL;
hr = ::CoCreateInstance(c1, NULL, CLSCTX_INPROC_SERVER, i1, (void **)&pInterface);
pInterface-> doSync();
pInterface->Release();
::CoUninitialize();
return 0;
}
I received this error message:
'doSync' : is not a member of 'IDispatch'
Here's my code:
#include <windows.h>
#include <Objbase.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
CLSID c1;
IID i1;
::CLSIDFromString(L"8718BC
::CLSIDFromString(L"8718BC
ISync : IDispatch* pInterface = NULL;
hr = ::CoCreateInstance(c1, NULL, CLSCTX_INPROC_SERVER, i1, (void **)&pInterface);
pInterface-> doSync();
pInterface->Release();
::CoUninitialize();
return 0;
}
Change
ISync : IDispatch* pInterface = NULL;
to
ISync *pInterface = NULL;
Or you need to call QueryInterface() on the pInterface to obtain ISync.
ISync : IDispatch* pInterface = NULL;
to
ISync *pInterface = NULL;
Or you need to call QueryInterface() on the pInterface to obtain ISync.
ASKER
The first way doesn't help, just give me the same error message . I have tried to call QueryInterface() by adding the few line below and got the same error message. Note, I used the same "interface ID" for CoCreateInstance, is it correct?
IUnknown *punk = NULL;
hr = ::CoCreateInstance(c1, NULL, CLSCTX_INPROC_SERVER, i1, (void **)&pInterface);
pInterface->QueryInterface (i1, (void**)&punk);
punk-> doSync();
error message:
"doSync' : is not a member of 'IUnknown'"
IUnknown *punk = NULL;
hr = ::CoCreateInstance(c1, NULL, CLSCTX_INPROC_SERVER, i1, (void **)&pInterface);
pInterface->QueryInterface
punk-> doSync();
error message:
"doSync' : is not a member of 'IUnknown'"
You need to query the ISync interface since doSync() is a member of ISync. Change IUnknown to ISync.
ASKER
I used 8718BC03-A658-11D5-A2C6-00 C04F81C073 , which is the id for ISync interface I believe. When I change IUnknown to ISync, i received an error message which said ISync is not define.
Since you don't have a header file that has ISync defined, you will have to use IDispatch and call IDispatch::Invoke() on the methods you want to call. The dispid for doSync() is 2 according to the info you showed above.
ASKER
does Invoke() only enable one method at a time?
btw, how do i write the code to define ISync in header file?
-->me dumb java programmer :), C++ is kind of cryptic to me.
btw, how do i write the code to define ISync in header file?
-->me dumb java programmer :), C++ is kind of cryptic to me.
Invoke() will call one method at a time. In order to call another method, you need to call Invoke() again with the dispid.
Do you have the source code of the COM object (MSync.Sync)? The header file is generated when you compile the IDL file.
Do you have the source code of the COM object (MSync.Sync)? The header file is generated when you compile the IDL file.
ASKER
hr = DISP_E_EXCEPTION when I do it this way:
-DISP_E_EXCEPTION The application needs to raise an exception. In this case, the structure passed in pExcepInfo should be filled in.
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
EXCEPINFO FAR *pExcepInfo;
IID i2;
hr=pInterface->Invoke(2,i2 ,NULL,DISP ATCH_METHO D,&disppar amsNoArgs, NULL,pExce pInfo,NULL );
plus according to help file, second parameter is "Reserved for future use; set to IID_NULL"
but I received "error LNK2019: unresolved external symbol GUID_NULL referenced in function WinMain"
when I put IID_NULL in second parameter and build the application(no error if I compile only).
-DISP_E_EXCEPTION The application needs to raise an exception. In this case, the structure passed in pExcepInfo should be filled in.
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
EXCEPINFO FAR *pExcepInfo;
IID i2;
hr=pInterface->Invoke(2,i2
plus according to help file, second parameter is "Reserved for future use; set to IID_NULL"
but I received "error LNK2019: unresolved external symbol GUID_NULL referenced in function WinMain"
when I put IID_NULL in second parameter and build the application(no error if I compile only).
ASKER
Should I put "0x00000002" instead of 2 in first parameter or it makes no different?
hr=pInterface->Invoke(2, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, NULL, NULL, NULL);
For IID_NULL, add uuid.lib to your linker options.
For IID_NULL, add uuid.lib to your linker options.
ASKER
I still get "DISP_E_EXCEPTION" when I invoke "doSync"
"HRESULT lastErrorCode([out, retval] long* pVal);"
and how do I invoke method with 1 out parameter like "lastErrorCode"
I get "DISP_E_MEMBERNOTFOUND" when I did the same thing as invoking "doSync" ?
"HRESULT lastErrorCode([out, retval] long* pVal);"
and how do I invoke method with 1 out parameter like "lastErrorCode"
I get "DISP_E_MEMBERNOTFOUND" when I did the same thing as invoking "doSync" ?
ASKER
Should I use this for "lastErrorCode"
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_BYREF|VT_BSTR;
dispparams.rgvarg[0].pbstr Val = &xmlInput;
dispparams.rgdispidNamedAr gs = NULL;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_BYREF|VT_BSTR;
dispparams.rgvarg[0].pbstr
dispparams.rgdispidNamedAr
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
Have you registered the COM DLL successfully on the CE device?
If you get DISP_E_EXCEPTION,
EXCEPINFO einfo;
hr=pInterface->Invoke(2, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, NULL, &einfo, NULL);
then have a look at the einfo.
If you get DISP_E_EXCEPTION,
EXCEPINFO einfo;
hr=pInterface->Invoke(2, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, NULL, &einfo, NULL);
then have a look at the einfo.
ASKER
I received "DoSync failed". My guess is, it's because I didn't have Oracle Lite on the desktop so it can't sync with it.
Now I need to know what values I need to assign to DISPPARAMS for "HRESULT lastErrorCode([out, retval] long* pVal);"
to work.Is this ok:
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_I4 | VT_BYREF ; // A reference to a 4-byte integer was passed
dispparams.rgdispidNamedAr gs = NULL;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
Now I need to know what values I need to assign to DISPPARAMS for "HRESULT lastErrorCode([out, retval] long* pVal);"
to work.Is this ok:
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_I4 | VT_BYREF ; // A reference to a 4-byte integer was passed
dispparams.rgdispidNamedAr
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
It should be
long lVal = 0;
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_I4 | VT_BYREF ; // A reference to a 4-byte integer was passed
dispparams.rgvarg[0].plVal = &lVal;
dispparams.rgdispidNamedAr gs = NULL;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
long lVal = 0;
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_I4 | VT_BYREF ; // A reference to a 4-byte integer was passed
dispparams.rgvarg[0].plVal
dispparams.rgdispidNamedAr
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
ASKER
ops, actually I need to transalate a method equalvalent to "lastErrorMessage" not "lastErrorCode".
dispinterface ISync{
-[id(0x00000005), propget, helpstring("property lastErrorMessage")]
-BSTR lastErrorMessage();}
interface ISync : IDispatch {
-[id(0x00000005), propget, helpstring("property lastErrorMessage")]
-HRESULT lastErrorMessage([out, retval] BSTR* pVal);}
I try LastErroMessage with this code:
BSTR lVal;
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_BYREF|VT_BSTR;
dispparams.rgvarg[0].pbstr Val = &lVal;
dispparams.rgdispidNamedAr gs = NULL;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
pInterface->Invoke(5, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &parms, NULL, &pExcepInfo, NULL);
And I received this runtime error:
-Fatal Application Error
-Application test3.exe has performed an illegal operation and will be shut down.
-Exception: 0xc0000005
dispinterface ISync{
-[id(0x00000005), propget, helpstring("property lastErrorMessage")]
-BSTR lastErrorMessage();}
interface ISync : IDispatch {
-[id(0x00000005), propget, helpstring("property lastErrorMessage")]
-HRESULT lastErrorMessage([out, retval] BSTR* pVal);}
I try LastErroMessage with this code:
BSTR lVal;
DISPPARAMS dispparams;
dispparams.rgvarg[0].vt = VT_BYREF|VT_BSTR;
dispparams.rgvarg[0].pbstr
dispparams.rgdispidNamedAr
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
pInterface->Invoke(5, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &parms, NULL, &pExcepInfo, NULL);
And I received this runtime error:
-Fatal Application Error
-Application test3.exe has performed an illegal operation and will be shut down.
-Exception: 0xc0000005
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, u are a great help. :)
ASKER