8

私は C#/Java のバックグラウンドを持っていたので、C# dll と同様に動作する C++ dll を作成する方法を見つけようとしています。

__declspec(dllexport)私はとを試しまし__declspec(dllimport)たが、静的メソッドでしか機能しませんでした。これは私の理解力の限界によるものだと思います。

クラスを C++ (プライベート メンバーを含む全体) でエクスポートし、C# の場合と同じように参照側でインスタンス化するにはどうすればよいですか? オンライン リソース/チュートリアルへのポインタでも実行できます。

私は MFC dll テンプレートを使い始めましたが、正直なところ、それらの 90% が何のためにあるのか、なぜ CWinApp から継承しているのかわかりません。クラスにタグを付けようとしましCCppPracticeLibraryAppたが、コンパイルできなくなりました。

// CppPracticeLibrary.h : main header file for the CppPracticeLibrary DLL
//


#pragma once

#ifndef __AFXWIN_H__
    #error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"       // main symbols

#ifdef CCppPracticeLibraryApp_EXPORTS
#define CCppPracticeLibraryApp_API __declspec(dllexport) 
#else
#define CCppPracticeLibraryApp_API __declspec(dllimport) 
#endif

// CCppPracticeLibraryApp
// See CppPracticeLibrary.cpp for the implementation of this class
//

class CCppPracticeLibraryApp : public CWinApp
{
public:
    CCppPracticeLibraryApp();
    static CCppPracticeLibraryApp_API void SayHelloWorld();
// Overrides
public:
    virtual BOOL InitInstance();

    DECLARE_MESSAGE_MAP()
};

定義ファイル:

//CppPracticeLibrary.cpp : DLL の初期化ルーチンを定義します。

#include "stdafx.h"
#include "CppPracticeLibrary.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define CCppPracticeLibraryApp_EXPORTS



BEGIN_MESSAGE_MAP(CCppPracticeLibraryApp, CWinApp)
END_MESSAGE_MAP()


// CCppPracticeLibraryApp construction

CCppPracticeLibraryApp::CCppPracticeLibraryApp()
{
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}

void CCppPracticeLibraryApp::SayHelloWorld()
{
    printf( "Hello world");
}


// The one and only CCppPracticeLibraryApp object

CCppPracticeLibraryApp theApp;


// CCppPracticeLibraryApp initialization

BOOL CCppPracticeLibraryApp::InitInstance()
{
    CWinApp::InitInstance();

    return TRUE;
}

クライアント/参照方法

// TestConsoleApplication.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "TestConsoleApplication.h"
#include "CppPracticeLibrary.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(NULL);

    if (hModule != NULL)
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: MFC initialization failed\n"));
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.
            /*CCppPracticeLibraryApp* testCallingLibrary =  new CCppPracticeLibraryApp();
            testCallingLibrary->SayHelloWorld();*/
            CCppPracticeLibraryApp::SayHelloWorld();
        }
    }
    else
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
    }

    return nRetCode;
}

上記のコードで次の行のコメントを解除できるようにしたいと思います。

        /*CCppPracticeLibraryApp* testCallingLibrary =  new CCppPracticeLibraryApp();
        testCallingLibrary->SayHelloWorld();*/
4

4 に答える 4

6

MSDNから

クラス内のすべてのパブリック データ メンバーとメンバー関数をエクスポートするには、次のようにクラス名の左側にキーワードを指定する必要があります。

class __declspec(dllexport) CExampleExport : public CObject
{ ... class definition ... };

また、ファイルなど、これを行う方法が他にもあることを考慮して.DEFください。MSDN サイトの説明をよく読んでください。

于 2013-05-21T06:54:06.477 に答える
4

この件に関しては、CodeProject に関する非常に興味深い記事を読む必要があります。

境界で C++ クラス(MFC クラス、または STL クラスを含む) を使用して DLL をビルドする場合、DLL クライアントは同じ VC++ コンパイラ バージョン同じ CRT フレーバー(たとえば、マルチスレッド DLL デバッグ CRT、マルチスレッド DLL リリース CRT、_HAS_ITERATOR_DEBUGGINGDLL を使用する EXE をビルドするための他の「より微妙な」設定 (同じ設定など)。

代わりに、純粋な C インターフェイスを DLL からエクスポートする場合(ただし、Win32 API と同様に、DLLでC++ を使用できます)、またはCOM DLLをビルドする場合、DLL クライアントは異なるバージョンの VC++ コンパイラを使用できます (さらに異なるCRT) を使用して DLL を使用します。

さらに、前述の記事で「C++ の成熟したアプローチ」と定義されていること(つまり、抽象インターフェイスを使用すること) にも注意してください。

于 2013-05-21T10:27:57.840 に答える
4

declspecクラスのすべてのメンバーをエクスポートするには、以下のように宣言に含めることができます。

class __declspec(dllexport) ExportedClass
{
    //....
};
于 2013-05-21T06:53:58.467 に答える