﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-newsea</title><link>http://www.cppblog.com/newsea/</link><description>苦海无涯，回头见岸</description><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 22:58:50 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 22:58:50 GMT</pubDate><ttl>60</ttl><item><title>[转]在非MFC程序中引用CString</title><link>http://www.cppblog.com/newsea/archive/2008/11/27/67987.html</link><dc:creator>NewSea</dc:creator><author>NewSea</author><pubDate>Thu, 27 Nov 2008 06:55:00 GMT</pubDate><guid>http://www.cppblog.com/newsea/archive/2008/11/27/67987.html</guid><wfw:comment>http://www.cppblog.com/newsea/comments/67987.html</wfw:comment><comments>http://www.cppblog.com/newsea/archive/2008/11/27/67987.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/newsea/comments/commentRss/67987.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/newsea/services/trackbacks/67987.html</trackback:ping><description><![CDATA[[http://www.vckbase.com/document/viewdoc/?id=1437] <br><br>在非MFC程序中引用CString <br><br>作者：阿半 <br><br>　　CString在当今软件设计界里还是小有名气的，说它是MFC中使用的最多的类一点也不过，然而在使用sdk编windows程序的时候，确不能利用CString类，只能用sdk的运行时库，比如strlen，strcpy，strstr等等，本文讨论的是在非mfc程序中使用CString类的方法，算是个引子，如果有更好的实现方法，欢迎大家讨论。 <br>　　为了使用CString类，首先源文件必须是以cpp结尾的，这是因为vc默认对不同的扩展名采用不同的编译方法和错误检查，mfc的支持文件Afx.h只有由cpp为扩展名的文件包含才能够正常编译。对于exe和库要采用不同的方式。下面分别讨论： <br><br>一、在非dll或者lib的工程里，使用CString非常容易，只要两步： <br><br>1、对于没有包含 &lt;Windows.h&gt; 的stdafx.h中，只要包含了afx.h即可，而对于已经包含了 &lt;windows.h&gt; 的stdafx.h, &nbsp; 一定需要保证afx.h在windows.h之前被包含。另外由于默认的控制台程序采用的单线程运行库，我们要把它改成多线程库，这些工作只要在stdafx.h中进行修改就可以了（详细信息可以参考候捷的 &lt; &lt;mfc &nbsp; 深入浅出&gt; &gt; ），我使用的一个stdafx.h的例子如下（这是一个从向导生成的win32位GUI的程序的stdafx.h修改的）： &nbsp; <br><br><br>&nbsp; &nbsp; // &nbsp; stdafx。h <br>&nbsp; &nbsp; #if &nbsp; !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_) <br>&nbsp; &nbsp; #define &nbsp; AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; #if &nbsp; _MSC_VER &nbsp; &gt; &nbsp; 1000 <br>&nbsp; &nbsp; #pragma &nbsp; once <br>&nbsp; &nbsp; #endif &nbsp; // &nbsp; _MSC_VER &nbsp; &gt; &nbsp; 1000 <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; #define &nbsp; WIN32_LEAN_AND_MEAN &nbsp; &nbsp; // &nbsp; Exclude &nbsp; rarely-used &nbsp; stuff &nbsp; from &nbsp; Windows &nbsp; headers <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; #include &nbsp; &lt;afx.h&gt; &nbsp; &nbsp; // &nbsp; 加在这里 <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; // &nbsp; Windows &nbsp; Header &nbsp; Files: <br>&nbsp; &nbsp; #include &nbsp; &lt;windows.h&gt; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; // &nbsp; C &nbsp; RunTime &nbsp; Header &nbsp; Files <br>&nbsp; &nbsp; #include &nbsp; &lt;stdlib.h&gt; <br>&nbsp; &nbsp; #include &nbsp; &lt;malloc.h&gt; <br>&nbsp; &nbsp; #include &nbsp; &lt;memory.h&gt; <br>&nbsp; &nbsp; #include &nbsp; &lt;tchar.h&gt; <br>&nbsp; <br>&nbsp; &nbsp; #ifdef &nbsp; _DEBUG <br>&nbsp; &nbsp; &nbsp; #pragma &nbsp; comment(lib, &nbsp; "libcmtd.lib ") <br>&nbsp; &nbsp; #else <br>&nbsp; &nbsp; &nbsp; #pragma &nbsp; comment(lib, &nbsp; "libcmt.lib ") <br>&nbsp; &nbsp; #endif &nbsp; <br>&nbsp; &nbsp; // &nbsp; Local &nbsp; Header &nbsp; Files <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; #endif &nbsp; // &nbsp; !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_) <br><br>这样就可以在程序中正常使用CString类了。 <br><br>二、在dll或者lib的工程中，由于afx.h默认带了一个DllMain，致使要使用CString类需要几个步骤。 <br><br>1、首先和控制台程序一样，如果编译环境设置了采用单线程库, &nbsp; 要改成多线程库，这个可以从工程属性里进行修改，详细信息可以参考候捷的 &lt; &lt;mfc &nbsp; 深入浅出&gt; &gt; . &nbsp; 下面给出的是我常用的方式，可以直接把它复制到工程里使用： <br>&nbsp; &nbsp; &nbsp; #ifdef &nbsp; _DEBUG <br>&nbsp; &nbsp; &nbsp; #pragma &nbsp; comment(lib, &nbsp; "libcmtd.lib ") <br>&nbsp; &nbsp; &nbsp; #else <br>&nbsp; &nbsp; &nbsp; #pragma &nbsp; comment(lib, &nbsp; "libcmt.lib ") <br>&nbsp; &nbsp; &nbsp; #endif &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>2、工程目录下创建一个DLLMODUL.CPP文件，并且把它加入到当前工程中。 <br>3、打开DLLMODUL.CPP文件，编辑这个文件为这样： &nbsp; &nbsp; &nbsp; &nbsp; #include &nbsp; "stdafx.h " <br>&nbsp; &nbsp; &nbsp; #ifdef &nbsp; _DEBUG <br>&nbsp; &nbsp; &nbsp; #undef &nbsp; THIS_FILE <br>&nbsp; &nbsp; &nbsp; static &nbsp; char &nbsp; THIS_FILE[] &nbsp; = &nbsp; __FILE__; <br>&nbsp; &nbsp; &nbsp; #endif <br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; #define &nbsp; new &nbsp; DEBUG_NEW <br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; ///////////////////////////////////////////////////////////////////////////// <br>&nbsp; &nbsp; &nbsp; // &nbsp; global &nbsp; data <br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; // &nbsp; The &nbsp; following &nbsp; symbol &nbsp; used &nbsp; to &nbsp; force &nbsp; inclusion &nbsp; of &nbsp; this &nbsp; module &nbsp; for &nbsp; _USRDLL <br>&nbsp; &nbsp; &nbsp; #ifdef &nbsp; _X86_ <br>&nbsp; &nbsp; &nbsp; extern &nbsp; "C " &nbsp; { &nbsp; int &nbsp; _afxForceUSRDLL; &nbsp; } <br>&nbsp; &nbsp; &nbsp; #else <br>&nbsp; &nbsp; &nbsp; extern &nbsp; "C " &nbsp; { &nbsp; int &nbsp; __afxForceUSRDLL; &nbsp; } <br>&nbsp; &nbsp; &nbsp; #endif &nbsp; &nbsp; <br>&nbsp; 4、打开stdafx.h，把afx.h包含在windows.h前面。现在可以正常的使用CString了。 
<img src ="http://www.cppblog.com/newsea/aggbug/67987.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/newsea/" target="_blank">NewSea</a> 2008-11-27 14:55 <a href="http://www.cppblog.com/newsea/archive/2008/11/27/67987.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>