混合托管代码和非托管代码

时间:2014-10-03 22:57:26

标签: c# c++ mixed-mode

这样做的目的是能够创建一个C#Form Application,其中按下一个按钮然后将一个句柄抛出到一个非托管代码部分以在该句柄上绘制一些内容。

我有什么。

- 具有此功能的本机C ++库

bool Worker::DrawText(HWND hWnd){


    PAINTSTRUCT ps;
    HDC hdc;
    hdc = BeginPaint(hWnd, &ps);
    TextOut(hdc, 0, 0, L"Hello, Windows!", 15);
    EndPaint(hWnd, &ps);
    return false;
}

托管C ++ DLL(CLR项目),具有此功能

bool ManagedClass::DrawBitmap(int iHwnd){
    if (nullptr != m_unMannedWorker){
        HWND hWnd = (HWND)iHwnd;
        return m_unMannedWorker->DrawText(hWnd);
    }
}

然后是C#事件代码,它通过托管代码包装器调用非托管代码部分

private void button1_Click(object sender, EventArgs e)
{
     ManagedClass m = new ManagedClass();
     m.DrawBitmap(this.Handle.ToInt32());
}

我可以很好地构建本机代码。当我尝试构建托管包装器时,我遇到以下链接器问题。

NativeLib2.lib(NativeSource.obj) : error LNK2019: unresolved external symbol __imp__TextOutW@20 referenced in function "public: bool __thiscall Worker::DrawTextW(struct HWND__ *)" (?DrawTextW@Worker@@QAE_NPAUHWND__@@@Z)

NativeLib2.lib(NativeSource.obj) : error LNK2019: unresolved external symbol __imp__BeginPaint@8 referenced in function "public: bool __thiscall Worker::DrawTextW(struct HWND__ *)" (?DrawTextW@Worker@@QAE_NPAUHWND__@@@Z)

NativeLib2.lib(NativeSource.obj) : error LNK2019: unresolved external symbol __imp__EndPaint@8       referenced in function "public: bool __thiscall Worker::DrawTextW(struct HWND__ *)" (?DrawTextW@Worker@@QAE_NPAUHWND__@@@Z)

为什么托管代码关心从已编译的lib函数中进行的调用?

有什么建议吗?

修改

根据建议,我检查了链接设置,但我现在处理以下链接错误

ManagedSource.obj : error LNK2028: unresolved token (0A00004B) "public: bool __thiscall Worker::DrawText(struct HWND__ *)" (?DrawText@Worker@@$$FQAE_NPAUHWND__@@@Z) referenced in function "public: bool __clrcall ManagedClass::DrawText(int)" (?DrawText@ManagedClass@@$$FQ$AAM_NH@Z)

ManagedSource.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Worker::DrawText(struct HWND__ *)" (?DrawText@Worker@@$$FQAE_NPAUHWND__@@@Z) referenced in function "public: bool __clrcall ManagedClass::DrawText(int)" (?DrawText@ManagedClass@@$$FQ$AAM_NH@Z)

注意:我已确保使用

链接本机lib文件
#pragma comment(lib, "NativeLib2.lib")

我还在set中包含了对托管DLL项目的非托管项目的引用

0 个答案:

没有答案