[問題] 請問console程式如何調用MFC

看板C_and_CPP作者 (交換關聯)時間15年前 (2010/09/10 17:35), 編輯推噓3(3025)
留言28則, 5人參與, 最新討論串1/2 (看更多)
希望能在console中調用mfc的DLL 試了很久還是沒結果啊。 我先創了一個預設的 MFC project(名就叫MFC),選用表單。 再把組態型態設為 dll 然後在MFC這project加了一個外面調用的API: CreateDLG.h #pragma once #include <windows.h> #define DLG_API __declspec(dllexport) DLG_API BOOL CreateDLG(void); CreateDLG.cpp DLG_API BOOL CreateDLG(void) { if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { printf("MFC failed to initialize!\n"); return FALSE; } CMFCDlg dlg; AfxGetApp()->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 } return TRUE; }/*BOOL*/ 然後再開一個空白的project,放個main進去,以調用CreateDLG這API: #include "CreateDLG.h" int main(void) { CreateDLG(); return 0; }/*main*/ 程式跑了,沒error/warning,但就是不會叫出那個表單啊。。。 那請問我要如何修改CreateDLG,讓console可以順利調用MFC呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.158.204 ※ 編輯: Gaiger 來自: 59.120.158.204 (09/10 17:35)

09/10 22:35, , 1F
沒有MainWindow, 如何DoModal()?
09/10 22:35, 1F

09/10 22:37, , 2F
然後沒用Console API Alloc/AttachConsole, 有錯誤訊息
09/10 22:37, 2F

09/10 22:37, , 3F
也沒辦法看到也是很正常的...
09/10 22:37, 3F

09/10 23:06, , 4F
什麼情形下,DLG_API會是 __declspec(dllimport)呢?
09/10 23:06, 4F

09/11 00:32, , 5F
請問一樓,那我該如何修改程式呢?
09/11 00:32, 5F

09/11 00:32, , 6F
小弟初學視窗程式設計,相當多不懂啊。
09/11 00:32, 6F

09/11 00:33, , 7F
還請高手指點 謝謝。
09/11 00:33, 7F

09/11 00:50, , 8F
google "mfc console"就有大量的sample了啊, 對解釋各種
09/11 00:50, 8F

09/11 00:50, , 9F
難處和解決方法還滿詳細的...
09/11 00:50, 9F

09/11 01:17, , 10F
囧 我查了半天都是 mfc 調用console的啊QQ
09/11 01:17, 10F

09/11 01:18, , 11F
不然就是mfc 的 console QQ
09/11 01:18, 11F

09/11 09:13, , 12F
09/11 09:13, 12F

09/11 09:14, , 13F
這是那兩個keyword的第4個result...
09/11 09:14, 13F

09/11 09:18, , 14F
用MFC調用console是比較正常的作法, 因為大部份MFC的
09/11 09:18, 14F

09/11 09:18, , 15F
UI class都expect有Main Window的存在.
09/11 09:18, 15F

09/11 09:19, , 16F
由其是Dialog. Win32中的Dialog不具備處理message loop
09/11 09:19, 16F

09/11 09:19, , 17F
Console呼叫MFC我想是行不通的 你硬要做的話請加油
09/11 09:19, 17F

09/11 09:20, , 18F
的功能, 都是把Windows message傳回上層的parent window
09/11 09:20, 18F

09/11 09:21, , 19F
的WinProc處理的. 沒有Main Window的程式想產生Dialog
09/11 09:21, 19F

09/11 09:22, , 20F
只能先產生一個不顯示的parent, 或者直接寫一個dialog
09/11 09:22, 20F

09/11 09:22, , 21F
模樣的Window來解決.
09/11 09:22, 21F

09/11 09:24, , 22F
MFC調用console只要處理好console API其實並不複雜...
09/11 09:24, 22F

09/11 09:35, , 23F

09/11 09:35, , 24F
mfc-tutorials/mfc-dll-tutorial.aspx
09/11 09:35, 24F

09/11 09:36, , 25F
上面有一句話是Create a new MFC AppWizard Executable
09/11 09:36, 25F

09/11 09:36, , 26F
application. Even a console application will be
09/11 09:36, 26F

09/11 09:36, , 27F
enough for using the MFC Dll.
09/11 09:36, 27F

09/11 12:01, , 28F
那個不跑MFC的UI class當然萬事大吉了... :P
09/11 12:01, 28F
文章代碼(AID): #1CYVlAPM (C_and_CPP)
文章代碼(AID): #1CYVlAPM (C_and_CPP)