2015-08-27 39 views
1

我有一個項目,我收到一個示例代碼,這是一個exe,基本上加載到一個對話框的兩個activeX控件。VC++將示例應用程序轉換爲DLL

我需要使一個DLL來使用該控件的功能。

這樣做的最佳策略是什麼? 我想我的DLL需要做一個窗口的實例。 當窗口構建時,獲取指向activeX控件的指針以使其發生。

這樣做的最佳策略是什麼?或者如果它甚至是可能的?

應用程序代碼: StartUp.h #PRAGMA一次

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

#include "resource.h"  // main symbols 



// CStartUpApp: 
// See StartUp.cpp for the implementation of this class 
// 

class CStartUpApp : public CWinApp 
{ 
public: 
    CStartUpApp(); 

// Overrides 
    public: 
    virtual BOOL InitInstance(); 

// Implementation 

    DECLARE_MESSAGE_MAP() 
}; 

extern CStartUpApp theApp; 

StartUpDlg.h

#pragma once 
#include "xnssdkdevicectrl.h" 
#include "xnssdkwindowctrl.h" 


// CStartUpDlg dialog 
class CStartUpDlg : public CDialog 
{ 
// Construction 
public: 
    CStartUpDlg(CWnd* pParent = NULL); // standard constructor 

// Dialog Data 
    enum { IDD = IDD_STARTUP_DIALOG }; 

    protected: 
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 


// Implementation 
protected: 
    HICON m_hIcon; 

    // Generated message map functions 
    virtual BOOL OnInitDialog(); 
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam); 
    afx_msg void OnPaint(); 
    afx_msg HCURSOR OnQueryDragIcon(); 
    DECLARE_MESSAGE_MAP() 

private: 
    // [ XNS ACTIVEX HELP ] 
    // ----------------------------------------------------------------------- 
    // XNS Device control and Window control variables 
    // ----------------------------------------------------------------------- 
    CXnssdkwindowctrl m_ctrlXnsWindow; // XnsWindow control 
    CXnssdkdevicectrl m_ctrlXnsDevice; // XnsDevice control 

public: 
    afx_msg void OnBnClickedOk(); 
}; 

StartUp.cpp

#include "stdafx.h" 
#include "StartUp.h" 
#include "StartUpDlg.h" 

#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 


// CStartUpApp 

BEGIN_MESSAGE_MAP(CStartUpApp, CWinApp) 
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp) 
END_MESSAGE_MAP() 


// CStartUpApp construction 

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


// The one and only CStartUpApp object 

CStartUpApp theApp; 


// CStartUpApp initialization 

BOOL CStartUpApp::InitInstance() 
{ 
    // InitCommonControlsEx() is required on Windows XP if an application 
    // manifest specifies use of ComCtl32.dll version 6 or later to enable 
    // visual styles. Otherwise, any window creation will fail. 
    INITCOMMONCONTROLSEX InitCtrls; 
    InitCtrls.dwSize = sizeof(InitCtrls); 
    // Set this to include all the common control classes you want to use 
    // in your application. 
    InitCtrls.dwICC = ICC_WIN95_CLASSES; 
    InitCommonControlsEx(&InitCtrls); 

    CWinApp::InitInstance(); 

    AfxEnableControlContainer(); 

    // Standard initialization 
    // If you are not using these features and wish to reduce the size 
    // of your final executable, you should remove from the following 
    // the specific initialization routines you do not need 
    // Change the registry key under which our settings are stored 
    // TODO: You should modify this string to be something appropriate 
    // such as the name of your company or organization 
    SetRegistryKey(_T("Local AppWizard-Generated Applications")); 

    CStartUpDlg dlg; 
    m_pMainWnd = &dlg; 
    INT_PTR nResponse = dlg.DoModal(); 
    if (nResponse == IDOK) 
    { 
     // TODO: Place code here to handle when the dialog is 
     // dismissed with OK 
    } 
    else if (nResponse == IDCANCEL) 
    { 
     // TODO: Place code here to handle when the dialog is 
     // dismissed with Cancel 
    } 

    // Since the dialog has been closed, return FALSE so that we exit the 
    // application, rather than start the application's message pump. 
    return FALSE; 
} 

StartUpDlg.cpp

#include "stdafx.h" 
#include "StartUp.h" 
#include "StartUpDlg.h" 


// [ XNS ACTIVEX HELP ] 
// ----------------------------------------------------------------------- 
// This files are installed in {$SDK path}\sample_code\include 
// You should include this files 
// ----------------------------------------------------------------------- 
#include "XnsCommon.h" 
#include "XnsDeviceInterface.h" 

#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 


// Macro for OutputDebugString() 
#define DBG_LOG(...) do{\ 
    CString strMessage = _T("");\ 
    strMessage.AppendFormat(_T("(%S:%d)"), __FUNCTION__, __LINE__); \ 
    strMessage.AppendFormat(__VA_ARGS__);\ 
    OutputDebugString(strMessage);\ 
}while(0); 

// Macro for AfxMessageBox() 
#define ERROR_BOX(...) do{\ 
    CString strMessage = _T("");\ 
    strMessage.Format(__VA_ARGS__);\ 
    AfxMessageBox(strMessage);\ 
}while(0); 


// CAboutDlg dialog used for App About 

class CAboutDlg : public CDialog 
{ 
public: 
    CAboutDlg(); 

// Dialog Data 
    enum { IDD = IDD_ABOUTBOX }; 

    protected: 
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 

// Implementation 
protected: 
    DECLARE_MESSAGE_MAP() 
}; 

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
} 

void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
    CDialog::DoDataExchange(pDX); 
} 

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
END_MESSAGE_MAP() 


// CStartUpDlg dialog 




CStartUpDlg::CStartUpDlg(CWnd* pParent /*=NULL*/) 
    : CDialog(CStartUpDlg::IDD, pParent) 
{ 
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 

void CStartUpDlg::DoDataExchange(CDataExchange* pDX) 
{ 
    CDialog::DoDataExchange(pDX); 
    DDX_Control(pDX, IDC_XNSSDKDEVICECTRL, m_ctrlXnsDevice); 
    DDX_Control(pDX, IDC_XNSSDKWINDOWCTRL, m_ctrlXnsWindow); 
} 

BEGIN_MESSAGE_MAP(CStartUpDlg, CDialog) 
    ON_WM_SYSCOMMAND() 
    ON_WM_PAINT() 
    ON_WM_QUERYDRAGICON() 
    //}}AFX_MSG_MAP 
    ON_BN_CLICKED(IDOK, &CStartUpDlg::OnBnClickedOk) 
END_MESSAGE_MAP() 


// CStartUpDlg message handlers 

BOOL CStartUpDlg::OnInitDialog() 
{ 
    CDialog::OnInitDialog(); 

    // Add "About..." menu item to system menu. 

    // IDM_ABOUTBOX must be in the system command range. 
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
    ASSERT(IDM_ABOUTBOX < 0xF000); 

    CMenu* pSysMenu = GetSystemMenu(FALSE); 
    if (pSysMenu != NULL) 
    { 
     CString strAboutMenu; 
     strAboutMenu.LoadString(IDS_ABOUTBOX); 
     if (!strAboutMenu.IsEmpty()) 
     { 
      pSysMenu->AppendMenu(MF_SEPARATOR); 
      pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
     } 
    } 

    // Set the icon for this dialog. The framework does this automatically 
    // when the application's main window is not a dialog 
    SetIcon(m_hIcon, TRUE);   // Set big icon 
    SetIcon(m_hIcon, FALSE);  // Set small icon 

    // TODO: Add extra initialization here 

    // [ XNS ACTIVEX HELP ] 
    // ----------------------------------------------------------------------- 
    // Initializes the DLL files. 
    // For this, XnsActiveX library requires config.xml, device.xml, 
    // and xns.xml files and the DLL file list should be mentioned 
    // in Xns.xml file. The path of the DLL file can not exceed 512 bytes 
    // in length. The XnsActiveX library searches for xns.xml using 
    // XnsSDKDevice.ocx installed in "{$SDK path}\Config" folder. 
    // ----------------------------------------------------------------------- 
    long nRet = m_ctrlXnsDevice.Initialize(); 
    if (nRet != ERR_SUCCESS) 
    { 
     DBG_LOG(_T("XnsSdkDevice:: Initialize() fail: errno=[%d]\n"), nRet); 
    } 

    // [ XNS ACTIVEX HELP ] 
    // ----------------------------------------------------------------------- 
    // Initializes the XnsSdkWindow control. 
    // Namely, this will specify the window handle in order to display 
    // images on the screen. 
    // ----------------------------------------------------------------------- 
    nRet = m_ctrlXnsWindow.Initialize(NULL, NULL); 
    DBG_LOG(_T("XnsSdkWindow:: Initialize() return=[%d](%s)\n"), 
     nRet, m_ctrlXnsDevice.GetErrorString(nRet)); 


    return TRUE; // return TRUE unless you set the focus to a control 
} 

void CStartUpDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
    if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
    { 
     CAboutDlg dlgAbout; 
     dlgAbout.DoModal(); 
    } 
    else 
    { 
     CDialog::OnSysCommand(nID, lParam); 
    } 
} 

// If you add a minimize button to your dialog, you will need the code below 
// to draw the icon. For MFC applications using the document/view model, 
// this is automatically done for you by the framework. 

void CStartUpDlg::OnPaint() 
{ 
    if (IsIconic()) 
    { 
     CPaintDC dc(this); // device context for painting 

     SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); 

     // Center icon in client rectangle 
     int cxIcon = GetSystemMetrics(SM_CXICON); 
     int cyIcon = GetSystemMetrics(SM_CYICON); 
     CRect rect; 
     GetClientRect(&rect); 
     int x = (rect.Width() - cxIcon + 1)/2; 
     int y = (rect.Height() - cyIcon + 1)/2; 

     // Draw the icon 
     dc.DrawIcon(x, y, m_hIcon); 
    } 
    else 
    { 
     CDialog::OnPaint(); 
    } 
} 

// The system calls this function to obtain the cursor to display while the user drags 
// the minimized window. 
HCURSOR CStartUpDlg::OnQueryDragIcon() 
{ 
    return static_cast<HCURSOR>(m_hIcon); 
} 


void CStartUpDlg::OnBnClickedOk() 
{ 
    // TODO: Add your control notification handler code here 
    OnOK(); 
} 

我的DLL頭

__declspec(dllexport) long init();//initilize app with activeX controls in window 
__declspec(dllexport) long getDlgInstance();//get dlg handle to be able to call activeX 
__declspec(dllexport) long connect(long handle);//do stuff 
+0

@Elemental我不你不明白你的問題嗎? –

+0

如果你需要啓動一個對話框並返回一個指向對話框的指針,它需要是一個非模態對話框,你的調用程序需要在退出之前正確關閉對話框 - 這是你想要的嗎?還是應該是模態對話框,並且在關閉對話框之前init函數不會返回? –

+0

它將由dll控制,所以它是我想要的非模態對話框選項。 –

回答

0

不應該是硬... 沒有見過你的代碼(我怎麼可能?),我假設你定義的函數返回的ActiveX控件的指針給來電者。 根據您之前詢問的問題來判斷,您知道如何製作DLL。得到所有你在一個新的DLL項目需要的代碼,只導出功能,你想從主機應用程序的訪問,就大功告成了

+0

添加了應用程序代碼。 我的主要問題讓我們說,我有一個初始化函數在我的dll,將啓動窗口,啓動ActiveX控件並返回一個句柄到Cdialog,我該怎麼做? –

+0

正如你所說。讓你的函數返回你想要的句柄。如果你還想獲得一個指向activex控件的引用/指針,你可以爲該函數添加額外的(out)參數。 你也可能想聲明一個宏來爲你處理'__declspec(import)'和'__declspec(export)'。這也是[這裏]解釋(https://msdn.microsoft.com/de-de/library/9xyb5w93%28v=VS.120%29.aspx) – Marty

0

你的DLL的代碼會看起來像

__declspec (dllexport) CStartUpDlg *ShowStartUpDlg() 
{ AFX_MANAGE_STATE(AfxGetStaticModuleState()); // in case the dialog is in the DLL's .rc file 
    // alternate to the above line: AfxSetResourceHandle(hInstance); // hInstance is what you get in the DLL's InitInstance() 

    CStartUpDlg *pDlg = new CStartUpDlg(); 
    if (pDlg == NULL) 
     return NULL; 
    if (!pDlg->Create(IDD_STARTUP_DLG)) 
     return NULL; 
    pDlg->ShowWindow(SW_SHOW); 
    return pDlg; // make sure you close the dialog and delete pDlg at the end of your program 
}