Bali C Bali C - 3 years ago 234
C++ Question

Visual C++ code not working in Code::Blocks

I have the following code that I am currently using to call functions from a C# Dll, which works perfectly in Visual C++.

#include <mscoree.h>
#include <stdio.h>
#pragma comment(lib, "mscoree.lib")

void Bootstrap()
ICLRRuntimeHost *pHost = NULL;
HRESULT hr = CorBindToRuntimeEx(L"v4.0.30319", L"wks", 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pHost);
printf("HRESULT:%x\n", hr);

// target method MUST be static int method(string arg)
DWORD dwRet = 0;
hr = pHost->ExecuteInDefaultAppDomain(L"c:\\temp\\test.dll", L"Test.Hello", L"SayHello", L"Person!", &dwRet);
printf("HRESULT:%x\n", hr);

hr = pHost->Stop();
printf("HRESULT:%x\n", hr);


int main()

The problem is, when I move this into Code::Blocks (which I am more familiar with - as the little C++ I have done has been native) throws a lot of compiler errors.

The original compiler errors were because it couldn't find the header
. I found this in the .NET SDK, so I copied it over to the mingw include directory which solved that, and then I did the same for all the other headers it couldn't find.

After copying over all the headers it then started giving a whole load of other errors, to do with the code in the headers I had just moved - nothing to do with the code below.

Why is Code::Blocks having such a hard time running this when VS runs it straight off the bat?


Answer Source

Code::Blocks is a great IDE for C++ programming, but you are clearly doing Windows programming here. Though it is the same programming language, compilers are not compatible among them.

Either if you have downloaded the CodeBlocks version with the gcc compiler, or the single CodeBlocks IDE, you need to configure CodeBlocks in order to use the MS C++ compiler. In order to do that, go to Settings >> Compiler and debugger >> Toolchain executables.

Also, in the same option, look for Search directories and place there the path to the MS C++ compiler headers.

Once that is done, you will be able to compile your program.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download