none
undefine __cplusplus RRS feed

  • Question

  • Hello,

    I'm doing a little C application using Microsoft Visual C++ 2005, to host the CLR Runtime and start an .Net application.

    The problem i'm facing is that i cant undefine '__cplusplus', and since i'm coding in C, when using the structures in mscoree.h header, the compiler only sees interfaces, and not structures.

    A simple example of my code:


    Code Block

    CLRRuntimeHost * ClrHost;
    HRESULT hrResult;

    hrResult = CorBindToRuntimeEx(NULL, NULL, NULL, &CLSID_CLRRuntimeHost, &IID_ICLRRuntimeHost, (void **)&ClrHost);

    ClrHost->Start(); /* error happens here, compiler says: "error C2039: 'Start' : is not a member of 'ICLRRuntimeHost'" */



    ...


    Could anyone explain me how to undefine the '__cplusplus define', because i have already tried everything.


    thanks.
    Thursday, October 11, 2007 5:03 AM

Answers

  • You can instruct Visual C++ to compile as C instead of C++ by setting the "Compile As" project setting.

    Right click on your project and select "Properties".  Then select "Configuration Properties" -> "C/C++" -> "Advanced".  You will see an option called "Compile As".  Here you can select either "Compile as C++ Code (/TP)" or "Compile as C Code (/TC)".  Selecting "Compile as C Code" will do precicely that.

    Using the C interface the following should work:

    Code Block
    #define COBJMACROS
    #define CINTERFACE
    #include <mscoree.h>

    ...

    ICLRRuntimeHost *clrHost;
    HRESULT hrResult;

    //Retrieve a pointer to the CLR process
    hrResult = CorBindToRuntimeEx(NULL, NULL, 0, &CLSID_CLRRuntimeHost,
                                  &IID_ICLRRuntimeHost, (void**)&clrHost);
       
    //Start the CLR
    ICLRRuntimeHost_Start(clrHost);



    If you want to stick w/ C++ though, then the following code will work:

    Code Block

    ICLRRuntimeHost *ClrHost;
    HRESULT hrResult;

    //Retrieve a pointer to the CLR process
    hrResult = CorBindToRuntimeEx(NULL, NULL, NULL, CLSID_CLRRuntimeHost,

                                  IID_ICLRRuntimeHost, (void**)&ClrHost);


    //Start the CLR
    ClrHost->Start();

       

    Thursday, October 11, 2007 10:28 PM

All replies

  • In Visual C++ 2005 the CLR only supports C++ not C - I would just compile your C code as C++.

    Thursday, October 11, 2007 5:09 AM
    Moderator
  • why do the mscoree.h have C defines then?

    Does that means that i can not load the CLR into a process using C?
    Thursday, October 11, 2007 5:14 AM

  • Thursday, October 11, 2007 1:59 PM
  • You can instruct Visual C++ to compile as C instead of C++ by setting the "Compile As" project setting.

    Right click on your project and select "Properties".  Then select "Configuration Properties" -> "C/C++" -> "Advanced".  You will see an option called "Compile As".  Here you can select either "Compile as C++ Code (/TP)" or "Compile as C Code (/TC)".  Selecting "Compile as C Code" will do precicely that.

    Using the C interface the following should work:

    Code Block
    #define COBJMACROS
    #define CINTERFACE
    #include <mscoree.h>

    ...

    ICLRRuntimeHost *clrHost;
    HRESULT hrResult;

    //Retrieve a pointer to the CLR process
    hrResult = CorBindToRuntimeEx(NULL, NULL, 0, &CLSID_CLRRuntimeHost,
                                  &IID_ICLRRuntimeHost, (void**)&clrHost);
       
    //Start the CLR
    ICLRRuntimeHost_Start(clrHost);



    If you want to stick w/ C++ though, then the following code will work:

    Code Block

    ICLRRuntimeHost *ClrHost;
    HRESULT hrResult;

    //Retrieve a pointer to the CLR process
    hrResult = CorBindToRuntimeEx(NULL, NULL, NULL, CLSID_CLRRuntimeHost,

                                  IID_ICLRRuntimeHost, (void**)&ClrHost);


    //Start the CLR
    ClrHost->Start();

       

    Thursday, October 11, 2007 10:28 PM
  • Thanks chuck, your saved me.

    I was already using the "Compile as C code" configuration, and the #define CINTERFACE solved my problem.

    Thanks again!
    Thursday, October 11, 2007 11:25 PM
  • Hello, I’m triying to load and execute managed code using methos as shown belong

    SAFEARRAY * psArr;//Assembly as array

          SAFEARRAY * Param;

    CComPtr <ICorRuntimeHost> spRuntimeHost;

          CComPtr <_AppDomain>      spAppDomain;

          CComPtr <IUnknown>             spUnk;

          CComPtr <_MethodInfo> MI;

          CorBindToRuntimeEx( NULL,                                                                                             L"wks",                                                                                                         STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN,

                CLSID_CorRuntimeHost ,

                IID_ICorRuntimeHost ,

                (void**)&spRuntimeHost);

          spRuntimeHost->Start();

          spRuntimeHost->CurrentDomain (&spUnk);

          spUnk->QueryInterface(&spAppDomain.p);

          try

          {

                MI = spAppDomain->Load_3(psArr)->GetEntryPoint();

                Param = MI->GetParameters();

                MI->Invoke_3 (NULL, Param);

          }

          catch (_com_error ex)

          {

                std::wcout<< ex.ErrorMessage();

          }

          SafeArrayDestroy(psArr);

    So if I invoke GUI application all good, but if I invoke Console application Invoke_3() function rise an exceptional situation…

    Who has any ideas how to run Console Application?

    Monday, October 29, 2007 9:44 AM