Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. Ref to ActiveX control using C

Ref to ActiveX control using C

Scheduled Pinned Locked Moved COM
comtutorialquestiondesign
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohnnyG
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • J JohnnyG

      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.

      S Offline
      S Offline
      soptest
      wrote on last edited by
      #2

      Example how to work with COM in "C":

      /* main.c */
      //other headers here ...
      //...
      #inluce "windows.h"
      #include "objx\\objx.h"
      //"objX" is a test object

      int 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"{
      #endif

      void __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

      J A 2 Replies Last reply
      0
      • S soptest

        Example how to work with COM in "C":

        /* main.c */
        //other headers here ...
        //...
        #inluce "windows.h"
        #include "objx\\objx.h"
        //"objX" is a test object

        int 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"{
        #endif

        void __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

        J Offline
        J Offline
        JohnnyG
        wrote on last edited by
        #3

        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?

        S 1 Reply Last reply
        0
        • J JohnnyG

          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?

          S Offline
          S Offline
          soptest
          wrote on last edited by
          #4

          Yes, you have to use MIDL compiler to generate those files, because #import directive is C++ specific. soptest

          J 1 Reply Last reply
          0
          • S soptest

            Yes, you have to use MIDL compiler to generate those files, because #import directive is C++ specific. soptest

            J Offline
            J Offline
            JohnnyG
            wrote on last edited by
            #5

            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?????

            1 Reply Last reply
            0
            • S soptest

              Example how to work with COM in "C":

              /* main.c */
              //other headers here ...
              //...
              #inluce "windows.h"
              #include "objx\\objx.h"
              //"objX" is a test object

              int 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"{
              #endif

              void __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

              A Offline
              A Offline
              Amit Dey
              wrote on last edited by
              #6

              This is really cool. soptest, you are the man!! You yard yellow years yieldingly; you yanked your yearning yoke.-Jeremy Falcon-CPite. Amit Dey sonork: 100:18407 msn: visualcdev

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups