2009-10-02 49 views
0

我有一個使用c/C++ .NET的現有項目。目前,我已經被賦予了一個任務來從我現有的代碼創建一個窗體。 因此我在現有的c/C++ projects.form.h中添加了新的項目窗體窗體應用程序,form.cpp已經自動創建。 現在我有問題從我的c文件調用窗口。 即使我不能從我的c程序中調用form.h文件。 有沒有解決這個問題的方法。 這裏列出的是編碼....如何從本地cpp調用Windows窗體

的login.c

int LoginMain(int id,int task) 
{ 
LoginClear(); 
LoginEntry(id,task);  
dp_in = 1; 
Rep(); 

//I WOULD LIKE TO CALL THE FORM AT THIS STAGE 


Cashier(); 
dp_in = 0; 
Login(); 
return(0);   
} 

form.cpp

[STAThreadAttribute] 
int main(array<System::String ^> ^args) 
{ 
// Enabling Windows XP visual effects before any controls are created 
Application::EnableVisualStyles();  
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it 
Application::Run(gcnew Form1());  
return 0;  
} 

回答

0

你必須使用COM揭露.NET代碼,C++代碼:

1. 用COM屬性標記要暴露的接口:

[ComVisible(true)] 
[Guid("5CBA864C-C063-4a47-2344-56AE016ABEE3")] 
[InterfaceType(ComInterfaceType.InterfaceIsDual)] 
public interface IExposedInterface 
{ 
    void ShowForm(); 
} 

2. 標記要與COM揭露實現屬性:

[ComVisible(true)] 
[Guid("BE337127-0DF7-2344-AD66-2338FE3926D8")] 
[ProgId("NETAssembly.ExposedInterface")] 
public sealed class ExposedInterface : IExposedInterface 
{ 
} 

3. 使用regasm.exe生成TLB文件: regasm.exe /tlb:NETAssembly.tlb NETAssembly.dll

4. 在C代碼:

#include "stdafx.h" 
#include <atlimage.h> 

#import "NETAssembly.tlb" named_guids 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    // Initialize COM. 
    CoInitialize(NULL); 

    NETAssembly::IExposedInterfacePtr pIExposedInterface; 
    HRESULT hr = pIExposedInterface.CreateInstance(NETAssembly::CLSID_IExposedInterface); 

    pIExposedInterface->ShowForm() 

    // Release object. 
    pIExposedInterface = NULL; 

    // Uninitialize COM. 
    CoUninitialize(); 

    return 0; 
} 
0

感謝您的幫助。 我發現我現有的程序沒有使用/ CLR,但已經創建的窗體使用/ CLR。 根據msdn: - 小心不要混合運行時庫的靜態和動態版本。在進程中運行時庫有多個副本可能會導致問題,因爲一個副本中的靜態數據不會與另一個副本共享。鏈接程序可以防止您在一個.exe文件中同時鏈接靜態和動態版本,但仍可以最終生成兩個(或更多)運行時庫副本。例如,與運行時庫的動態(DLL)版本鏈接的.exe文件一起使用時,與運行時庫的靜態(非DLL)版本鏈接的動態鏈接庫會導致問題。 (您也應避免在一個進程中混合調試庫和非調試版的庫)。