Ref to ActiveX control using C
-
Hope this is the right place to ask this question. Does anyone have a good example or link to some C source code on how I can use an ActiveX control? I have a Win32 program in C that I would like to add two controls to. I looked at the articles in this section and did a search on the web and MSDN but it's kind of hard to find something that is for C only. I have books on how to design COM objects and ActiveX controls but no examples on how to use them in C.
-
Hope this is the right place to ask this question. Does anyone have a good example or link to some C source code on how I can use an ActiveX control? I have a Win32 program in C that I would like to add two controls to. I looked at the articles in this section and did a search on the web and MSDN but it's kind of hard to find something that is for C only. I have books on how to design COM objects and ActiveX controls but no examples on how to use them in C.
Example how to work with COM in "C":
/* main.c */
//other headers here ...
//...
#inluce "windows.h"
#include "objx\\objx.h"
//"objX" is a test objectint main(int argc, char* argv[])
{
IObj *pObj;
HRESULT hr;
BSTR wsIn;CoInitialize(0); hr = CoCreateInstance( &CLSID\_Obj, 0, CLSCTX\_SERVER, &IID\_IObj, (void\*\*) &pObj); if(S\_OK == hr) { wsIn = SysAllocString(L"Test string."); //all calls made using vtbl hr = pObj->lpVtbl->Test(pObj,wsIn); //see objX.h file for declaration of IObj interface pObj->lpVtbl->Release(pObj); SysFreeString(wsIn); } CoUninitialize();
}
must link with "ObjX_i.c" . This is my test object header file:
#pragma warning( disable: 4049 ) /* more than 64k source lines */
/* this ALWAYS GENERATED file contains the definitions for the interfaces */
/* File created by MIDL compiler version 5.03.0280 */
/* at Mon Jun 24 10:55:15 2002
*/
/* Compiler settings for C:\TEMP\tt\objX\objX.idl:
Oicf (OptLev=i2), W1, Zp8, env=Win32 (32b run), ms_ext, c_ext
error checks: allocation ref bounds_check enum stub_data
VC __declspec() decoration level:
__declspec(uuid()), __declspec(selectany), __declspec(novtable)
DECLSPEC_UUID(), MIDL_INTERFACE()
*/
//@@MIDL_FILE_HEADING( )/* verify that the version is high enough to compile this file*/
#ifndef __REQUIRED_RPCNDR_H_VERSION__
#define __REQUIRED_RPCNDR_H_VERSION__ 440
#endif#include "rpc.h"
#include "rpcndr.h"#ifndef __RPCNDR_H_VERSION__
#error this stub requires an updated version of
#endif // __RPCNDR_H_VERSION__#ifndef COM_NO_WINDOWS_H
#include "windows.h"
#include "ole2.h"
#endif /*COM_NO_WINDOWS_H*/#ifndef __objX_h__
#define __objX_h__/* Forward Declarations */
#ifndef __IObj_FWD_DEFINED__
#define __IObj_FWD_DEFINED__
typedef interface IObj IObj;
#endif /* __IObj_FWD_DEFINED__ */#ifndef __Obj_FWD_DEFINED__
#define __Obj_FWD_DEFINED__#ifdef __cplusplus
typedef class Obj Obj;
#else
typedef struct Obj Obj;
#endif /* __cplusplus */#endif /* __Obj_FWD_DEFINED__ */
/* header files for imported files */
#include "oaidl.h"
#include "ocidl.h"#ifdef __cplusplus
extern "C"{
#endifvoid __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t);
void __RPC_USER MIDL_user_free( void __RPC_FAR * );#ifndef __IObj_INTERFACE_DEFINED__
#define __IObj_INTERFACE_DEFINED__/* interface IObj */
/* [unique][helpstring][dual][uuid][obje -
Example how to work with COM in "C":
/* main.c */
//other headers here ...
//...
#inluce "windows.h"
#include "objx\\objx.h"
//"objX" is a test objectint main(int argc, char* argv[])
{
IObj *pObj;
HRESULT hr;
BSTR wsIn;CoInitialize(0); hr = CoCreateInstance( &CLSID\_Obj, 0, CLSCTX\_SERVER, &IID\_IObj, (void\*\*) &pObj); if(S\_OK == hr) { wsIn = SysAllocString(L"Test string."); //all calls made using vtbl hr = pObj->lpVtbl->Test(pObj,wsIn); //see objX.h file for declaration of IObj interface pObj->lpVtbl->Release(pObj); SysFreeString(wsIn); } CoUninitialize();
}
must link with "ObjX_i.c" . This is my test object header file:
#pragma warning( disable: 4049 ) /* more than 64k source lines */
/* this ALWAYS GENERATED file contains the definitions for the interfaces */
/* File created by MIDL compiler version 5.03.0280 */
/* at Mon Jun 24 10:55:15 2002
*/
/* Compiler settings for C:\TEMP\tt\objX\objX.idl:
Oicf (OptLev=i2), W1, Zp8, env=Win32 (32b run), ms_ext, c_ext
error checks: allocation ref bounds_check enum stub_data
VC __declspec() decoration level:
__declspec(uuid()), __declspec(selectany), __declspec(novtable)
DECLSPEC_UUID(), MIDL_INTERFACE()
*/
//@@MIDL_FILE_HEADING( )/* verify that the version is high enough to compile this file*/
#ifndef __REQUIRED_RPCNDR_H_VERSION__
#define __REQUIRED_RPCNDR_H_VERSION__ 440
#endif#include "rpc.h"
#include "rpcndr.h"#ifndef __RPCNDR_H_VERSION__
#error this stub requires an updated version of
#endif // __RPCNDR_H_VERSION__#ifndef COM_NO_WINDOWS_H
#include "windows.h"
#include "ole2.h"
#endif /*COM_NO_WINDOWS_H*/#ifndef __objX_h__
#define __objX_h__/* Forward Declarations */
#ifndef __IObj_FWD_DEFINED__
#define __IObj_FWD_DEFINED__
typedef interface IObj IObj;
#endif /* __IObj_FWD_DEFINED__ */#ifndef __Obj_FWD_DEFINED__
#define __Obj_FWD_DEFINED__#ifdef __cplusplus
typedef class Obj Obj;
#else
typedef struct Obj Obj;
#endif /* __cplusplus */#endif /* __Obj_FWD_DEFINED__ */
/* header files for imported files */
#include "oaidl.h"
#include "ocidl.h"#ifdef __cplusplus
extern "C"{
#endifvoid __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t);
void __RPC_USER MIDL_user_free( void __RPC_FAR * );#ifndef __IObj_INTERFACE_DEFINED__
#define __IObj_INTERFACE_DEFINED__/* interface IObj */
/* [unique][helpstring][dual][uuid][obje -
Thanks:) I'm wondering if I'm using say the serial comm port control then I wouldn't need or have that equivalent file for ObjX_i.c, right? Wouldn't I just import the typelib or something? Or, do I still need the MIDL compiler to generate the .c file?
-
Yes, you have to use MIDL compiler to generate those files, because #import directive is C++ specific. soptest
Could you help with this? If I used VC++ to add the component it generates the .tli and .tlh files but they don't seem usable for C. How do I generate a header file that will have all of the declarations for the mscomm32.ocx control? Does anyone have an example of how to take one of Microsoft's supplied controls e.g. mscomm32.ocx or msscript.ocx in a Win32 C program?????
-
Example how to work with COM in "C":
/* main.c */
//other headers here ...
//...
#inluce "windows.h"
#include "objx\\objx.h"
//"objX" is a test objectint main(int argc, char* argv[])
{
IObj *pObj;
HRESULT hr;
BSTR wsIn;CoInitialize(0); hr = CoCreateInstance( &CLSID\_Obj, 0, CLSCTX\_SERVER, &IID\_IObj, (void\*\*) &pObj); if(S\_OK == hr) { wsIn = SysAllocString(L"Test string."); //all calls made using vtbl hr = pObj->lpVtbl->Test(pObj,wsIn); //see objX.h file for declaration of IObj interface pObj->lpVtbl->Release(pObj); SysFreeString(wsIn); } CoUninitialize();
}
must link with "ObjX_i.c" . This is my test object header file:
#pragma warning( disable: 4049 ) /* more than 64k source lines */
/* this ALWAYS GENERATED file contains the definitions for the interfaces */
/* File created by MIDL compiler version 5.03.0280 */
/* at Mon Jun 24 10:55:15 2002
*/
/* Compiler settings for C:\TEMP\tt\objX\objX.idl:
Oicf (OptLev=i2), W1, Zp8, env=Win32 (32b run), ms_ext, c_ext
error checks: allocation ref bounds_check enum stub_data
VC __declspec() decoration level:
__declspec(uuid()), __declspec(selectany), __declspec(novtable)
DECLSPEC_UUID(), MIDL_INTERFACE()
*/
//@@MIDL_FILE_HEADING( )/* verify that the version is high enough to compile this file*/
#ifndef __REQUIRED_RPCNDR_H_VERSION__
#define __REQUIRED_RPCNDR_H_VERSION__ 440
#endif#include "rpc.h"
#include "rpcndr.h"#ifndef __RPCNDR_H_VERSION__
#error this stub requires an updated version of
#endif // __RPCNDR_H_VERSION__#ifndef COM_NO_WINDOWS_H
#include "windows.h"
#include "ole2.h"
#endif /*COM_NO_WINDOWS_H*/#ifndef __objX_h__
#define __objX_h__/* Forward Declarations */
#ifndef __IObj_FWD_DEFINED__
#define __IObj_FWD_DEFINED__
typedef interface IObj IObj;
#endif /* __IObj_FWD_DEFINED__ */#ifndef __Obj_FWD_DEFINED__
#define __Obj_FWD_DEFINED__#ifdef __cplusplus
typedef class Obj Obj;
#else
typedef struct Obj Obj;
#endif /* __cplusplus */#endif /* __Obj_FWD_DEFINED__ */
/* header files for imported files */
#include "oaidl.h"
#include "ocidl.h"#ifdef __cplusplus
extern "C"{
#endifvoid __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t);
void __RPC_USER MIDL_user_free( void __RPC_FAR * );#ifndef __IObj_INTERFACE_DEFINED__
#define __IObj_INTERFACE_DEFINED__/* interface IObj */
/* [unique][helpstring][dual][uuid][obje