在Windows Console型的程序中,不用MFC,如何获得应用的Instance值?

shimx 2005-01-21 11:18:13
在MFC中,有一个函数AfxGetInstanceHandle可以得到,但现在不用MFC,而console型的程序中,main函数传入参数中hInstance值不可见.
有什么Windows API函数或者方法吗?
...全文
259 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
delphihero 2005-01-22
  • 打赏
  • 举报
回复
比较敏感,口气冲了不好意思。对于有窗口的程序下面都可以得到句柄地址:
HINSTANCE ht= AfxGetInstanceHandle() ;
CString str;
str.Format("%d",ht);
AfxMessageBox(str);
HMODULE HMD = GetModuleHandle(NULL);
str.Format("%d",HMD);
AfxMessageBox(str);
HWND hwnd = AfxGetMainWnd()->m_hWnd;
str.Format("%d",hwnd);
AfxMessageBox(str);
long lg = GetWindowLong(hwnd,GWL_HINSTANCE);
str.Format("%d",lg);
AfxMessageBox(str);
其实都是400000为之开始,+ 一个固定偏移地址(大概是dos显示win32应用的)。
控制台程序就是400000开始的。它的句柄应该是控制台句柄。(我这么觉得,不认同我也没办法)
调用 GetModuleHandle()和GetWindowLong()是两个完全不同结果。
也没什么好争得,我说的控制台应用句柄。你的是程序模块加载地址。
或许控制台就没有应用程序句柄的说法。
别生气,俺不要你的可用分,只跟你比较一下嘛。至于技术能力在下不敢比较,早对这方面心灰意冷。女怕嫁错郎,男怕入错行。
漂流的代码 2005-01-22
  • 打赏
  • 举报
回复
GetCurrentProcess
fanqing 2005-01-21
  • 打赏
  • 举报
回复
gz
dandycheung 2005-01-21
  • 打赏
  • 举报
回复
我从来没有看他是什么值,难道是一个固定值就不能用吗?我不知道你所谓的“实际句柄”是什么东西。之所以是一个固定的 0x400000 是因为当前的操作系统实现就是把主执行模块映射到那个地址,可谁也不敢保证这个值以后不会变。
delphihero 2005-01-21
  • 打赏
  • 举报
回复
楼上的是固定 0x400000 的那个虚拟地址,不是实际句柄
dandycheung 2005-01-21
  • 打赏
  • 举报
回复
HINSTANCE hInstance = GetModuleHandle(NULL);
dandycheung 2005-01-21
  • 打赏
  • 举报
回复
楼上的小兄弟,从你说话的口气可以看出你的岁数还不大。和别人说话最好客气点。仔细看一下搂主的要求好不好?搂主说,他在 MFC 里可以用 AfxGetInstanceHandle() 函数,而在 console 程序里面也想得到同样的东西,者说的显然是模块句柄,我实在不明白你为什么非要和窗口什么的扯上关系呢?还有,在 csdn 上注册了多少年的事情是你先提出的,我只不过回了你一下,又没有要和你比什么的意思,而且在 csdn 上注册的早晚并不能代表一个人的水平,你说对不对?不敢多说,我写程序 11 年了,在 Windows 上编程也有 8 年了,在我身边承蒙大家看得起,也都还认为我的技术不差。我不会相信对 Windows 你比我多了解什么的。最后再奉劝一句,如果你在现实生活中也这样和别人交流,实在是不好。
delphihero 2005-01-21
  • 打赏
  • 举报
回复
得到模块句柄的用 I_Love_CPP(我爱C++) ,
由hwnd转过去:HINSTANCE hInstance = (HINSTANCE) ::GetWindowLongPtr(hwnd,GWL_HINSTANCE);
日,我有没说我的是 application instance。 前面已经有了这个函数了,我不过补充个参数

你的那个函数搞错了还拽个屁。csdn开了我就注册了。
强烈鄙视不懂乱写的人。
dandycheung 2005-01-21
  • 打赏
  • 举报
回复
你连模块句柄和窗口句柄都搞不清楚,还和我说什么呀。不要想当然,你看见搂主说要和其他进程通讯了吗?顺便说一句,我的 ID 是 2000 年注册的。
delphihero 2005-01-21
  • 打赏
  • 举报
回复
GetMoudleHandle() 回来的句柄对其他进程是没用的,它记录的是加载到当前进程的位置,如果第一个dll这个就是0x100... ,这都是虚拟内存里面地。你在另一个进程中是无法访问这个地址的,另一个进程中0x100...是那个进程的第一个dll位置。
而一般跟其他进程通信,都是FindWindow().....那个句柄才能反映另一个进程窗口情况。才是可以SendMessage()的对象。
你的可用分有多少阿。我的1w6,注册4年了。再给我也没意见

可以先改变控制台名字( 默认的含路径不好输入:( )然后FindWindow
SetConsoleTitle("hello");
HWND hwnd = FindWindow(NULL,"hello");
SendMessage(hwnd,WM_CLOSE,0,0);//关闭控制台,也可以在其他程序里面给这个句柄发消息
system("PAUSE"); //这个语句不会被执行
这个就是那个控制台程序窗口句柄。绝对不是0x40...
楼主意思也是找个窗口句柄吧
I_Love_CPP 2005-01-21
  • 打赏
  • 举报
回复
delphihero(阿蒙(C++)) 的说法是有道理的:

GetModuleHandle:

This function must be used carefully in a multithreaded application. There is no guarantee that the module handle remains valid between the time this function returns the handle and the time it is used.
For example, a thread retrieves a module handle, but before it uses the handle, a second thread frees the module. If the system loads another module, it could reuse the module handle that was recently freed. Therefore, first thread would have a handle to a module different than the one intended.

上文来自《MSDN》
dandycheung 2005-01-21
  • 打赏
  • 举报
回复
1、我不知道为什么要用到实际内存
2、我没有搞清楚实际内存对 SendMessage() 有什么帮助
还请楼上不吝赐教。如果能让我弄明白而且正确,我情愿把我所有的可用分都给你。
delphihero 2005-01-21
  • 打赏
  • 举报
回复
连虚拟内存跟实际内存都没搞清楚。固定的 0x40... 得到有用吗?到其他进程里SendMessage()对这个句柄根本没戏
I_Love_CPP 2005-01-21
  • 打赏
  • 举报
回复
GWL_HINSTANCE
Retrieves a handle to the application instance.
使用方法:
HINSTANCE hInstance = GetWindowLong (hwnd, GWL_HINSTANCE);
其中,hwnd为主窗口的句柄。

Contents Chapter 1: The Microsoft Developer Studio The Microsoft Developer Studio The Project Workspace Project Workspace Window Project Configurations Managing Complex Projects Project Settings Converting Projects Source Code Files Resource Scripts ResourceView Creating New Resources Identifying Resources Dialog Boxes String Tables Accelerator Tables Menus Icons, Bitmaps and Cursors Version Resources Custom Resources The Visual C++ Compiler Compiling on the Command Line The Foundation Classes MFC Source Code Header Files MFC Libraries Summary Chapter 2: The Wizards and The Gallery AppWizard Starting AppWizard Choosing Your Application's User Interface Selecting Database Support Adding Compound Document Support Embellishing Your User Interface Adding Advanced Features Miscellaneous Options Class Names One More Step Other Application Interfaces Choose Carefully! Compiling Your Application Precompiled Header Files ClassWizard Creating a New Class The .clw File The Browser Browser Files Browsing Shortcuts Components and Controls Gallery Summary Chapter 3: The Application Architecture Hierarchy The Application Framework Generating an Application with AppWizard Understanding the Generated Code CDocument and CView CWinApp CWinThread Locating Threads CCmdTarget Commands for Classes About Message Maps How are Message Maps Created? The BEGIN_MESSAGE_MAP() Macro Inside the Message Map Filling the Holes The END_MESSAGE_MAP() Macro Unfolding the Map There's No Sense of Obligation CObject Memory Management Debugging Support Serialization Run-time Type Information Your Own Classes and CObject The Big Picture: A New Life The WinMain() Function MFC's Message Pump Application Termination Summary Chapter 4: The Document/View Architecture Documents and Views Document/View Designs The Different Views Types of Document Document/View Consciousness What are Document Templates? CSingleDocTemplate S
1,01.zipOutput显示所有的调试信息(5KB)2,02.zipSome general debugging tips一般的调试技巧(11KB)3,03.zipDebugging ISAPI extension调试ISAPI扩展(4KB)4,04.zipLibDump类似DumpBin的工具(10KB)5,05.zipFinding memory leaks发现内存的泄漏(6KB)6,06.zipConvert message ID to a string将消息标志符转换成字符串(4KB)7,07.zipMessage Tracer消息跟踪(5KB)8,08.zipA simple profiler class一个简单的轮廓类(5KB)9,09.zipTerminator应用程序(5KB)10,10.zipTranslate Window Style转换窗口风格(5KB)11,11.zipLong String Debugging Macro调试宏(5KB)12,12.zipCheck for loaded DLLs检查装入的动态链接库(4KB)13,13.zipAutoincreasing build number自动增加版本信息的宏(5KB)14,14.zipFile Dialog Macro文件对话框宏(6KB)15,15.zipCode Template add-in for Visual C++ 5.0在VC5可增加的代码模板(5KB)16,16.zipComment / Uncomment macros命令/反命令宏(5KB)17,17.zipCustom built files自定义生成的文件(5KB)18,18.zipDefine Method定义方法(5KB)19,19.zipExport Makefile输出工程制作文件(5KB)20,20.zipJump to Next/Previous Function Definition跳转到下一个/上一个功能定义(4KB)21,21.zipInverting Assignment Operations转化操作任务(5KB)22,22.zipVC4.2 style keyboard macro recorder for DevStudio 97在DevStudio 97使用在VC4.2风格的键盘宏记录(4KB)23,23.zipApplication Launcher程序发射器, 能够在Word, Excel, Access, Power Point, Visio和Html连接网站(6KB)24,24.zipOpen current header file打开当前文件头(7KB)25,25.zipOpen header file打开文件头(4KB)26,26.zipSequentially Renumber Resource ID's重新对资源标识符编号(5KB)27,27.zipA secondary clipboard实现第二个剪贴板, 不过它用的热键是Ctrl+Shift+C, Ctrl+Shift+V和Ctrl+Shift+X(4KB)28,28.zipCase switching这个宏实现文字的大小写互换(4KB)29,29.zipWM_COMMAND user message macroWM_COMMAND用户消息宏(5KB)30,30.zipBetter caret movement by words在IDE移动快速单词的宏(5KB)

15,976

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧