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

4

2 に答える 2

5

Code::Blocksは C++ プログラミング用の優れた IDE ですが、ここで Windows プログラミングを行っていることは明らかです。同じプログラミング言語ですが、コンパイラは互換性がありません。

gcc コンパイラで CodeBlocks バージョンをダウンロードした場合、または単一の CodeBlocks IDE をダウンロードした場合は、MS C++ コンパイラを使用するために CodeBlocks を構成する必要があります。これを行うには、Settings >> Compiler and debugger >> Toolchain executablesに移動します。

また、同じオプションで、検索ディレクトリを探し、そこに MS C++ コンパイラ ヘッダーへのパスを配置します。

これが完了すると、プログラムをコンパイルできるようになります。

于 2013-01-14T11:17:30.473 に答える
0

Code::Blocks は、Visual Studio とはコンパイラが全く異なり、コンパイル時のソース コードのデコードとエンコードが異なり、同じプログラミング言語であるにもかかわらず、相互に認識できません。

于 2016-10-23T00:45:26.970 に答える