2

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);
    pHost->Start();
    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);

    pHost->Release();
}

int main()
{
    Bootstrap();
}

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 mscoree.h. 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?

Thanks

9
  • Can you post some of the errors? Have you set up the project to link with mscoree.dll? Commented Jan 14, 2013 at 10:37
  • I will try and find them again, from memory it's function not defined errors in various headers. How do I link the project to mscoree.dll? Commented Jan 14, 2013 at 10:39
  • Function definitions not being found definitely sounds like a linker error. I'm haven't really ever used Code::Blocks personally, but perhaps this will help? learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks Commented Jan 14, 2013 at 10:41
  • I'd also recommend against copying SDK headers; add the directory they are in your Compiler Directories. The same thing about mscoree.lib. Commented Jan 14, 2013 at 10:51
  • @Yuushi Thanks Yuushi, I will check out that and see if it fixes it. Commented Jan 14, 2013 at 11:07

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. I have switched to VS C++ compiler and added the directory into the include directories but after compiling I got 50 errors and 139 warnings. Any ideas?
Here you have an explanation of how to use Visual C 6 with CodeBlocks. The process is very similar. wiki.codeblocks.org/…
0

Code::Blocks has a different compiler altogether from Visual Studio, the decoding and encoding on source code during compilation is different and cannot recognize each other though they are same programming language.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.