日历
网志分类
展开全部
· ***    All     ***
· ***    Mood    ***
· ***    Life    ***
· *** Technology ***
· ***     Yc     ***
· ***  Cartoon   ***
· *** Collection ***
· ***  Unsorted  ***
媒体播放器

Get the Flash Player to see this player.
SkyDrive is currently not available.

站内搜索
友情链接
· 我的歪酷 非非共享界
· 风雨妖虹
· kingbeful@csdn
· 手心的太阳@瞬间十年
· Moment@Travis
· 江南麒麟居
· εз毛线团εз
· 乌拉的neverland
· 碾过的日子 闲也陶陶
· 狡兔三窟 *^.^*
· 橄榄林的风
· 水色の街
· Powerful and delicate, Life struggling
· 阿德咖吧
· 陷阱
· 风之华
· vkobe的Neverland@@
· 白日梦已死 · 伤越夜海
· 望天
· 空の軌跡
· 没什么好东西的空间
· 心情...咖啡屋
· 随风独自凉
· Some where i belong
· .★·°双晨·故事°☆ .
· BigWorld的记事本
· *Sara's*
· 人生若只如初见
· 宠辱不惊闲看庭前花开花落·去留无意漫随天外云卷云舒
· 我思我不在
· 飞扬飘雨
· lazy的猫猫
常用链接
· [Google]
· [Google Accounts]
· [IT Items]
· [Telnet@Yanxi]
· [Wikipedia]
· [Linux Manpages]
· [Mofile.com]
· [163888.net]
· [fm.qq.com]
· [Proxy]
· [Animepaper.net]
· [FreeproxySite]
· [gonwan@fc2]
· [skydrive.live.com]
· [gonwan@lifelogger]

订阅 RSS

0109297

歪酷博客

逆さまの蝶
In this Craziness
Uncertainy
一人一人の想いを
僕らは何処かに遺せるだろうか

In this Craziness
You gave me life
一つの想いを
僕らは何処まで守れるだろうか



« 上一篇: c/c++ xml parsers 下一篇: c/c++ xml parsers (2) »
丸子·酱 @ 2008-07-19 16:29

http://msdn.microsoft.com/en-us/library/ms235460(VS.80).aspx
意思就是说不要在dll间传FILE之类的CRT指针 - -b...

Potential Errors Passing CRT Objects Across DLL Boundaries 

When you pass C Run-time (CRT) objects such as file handles, locales, and environment variables into or out of a DLL (function calls across the DLL boundary), unexpected behavior can occur if the DLL, as well as the files calling into the DLL, use different copies of the CRT libraries.

A related problem can occur when you allocate memory (either explicitly with new or malloc, or implicitly with strdup, strstreambuf::str, and so on) and then pass a pointer across a DLL boundary to be freed. This can cause a memory access violation or heap corruption if the DLL and its users use different copies of the CRT libraries.

Another symptom of this problem can be an error in the output window during debugging such as:

HEAP[]: Invalid Address specified to RtlValidateHeap(#,#)

Causes

Each copy of the CRT library has a separate and distinct state. As such, CRT objects such as file handles, environment variables, and locales are only valid for the copy of the CRT where these objects are allocated or set. When a DLL and its users use different copies of the CRT library, you cannot pass these CRT objects across the DLL boundary and expect them to be picked up correctly on the other side.

Also, because each copy of the CRT library has its own heap manager, allocating memory in one CRT library and passing the pointer across a DLL boundary to be freed by a different copy of the CRT library is a potential cause for heap corruption.

If you design your DLL so that it passes CRT objects across the boundary or allocates memory and expects it to be freed outside the DLL, you restrict the DLL users to use the same copy of the CRT library as the DLL. The DLL and its users use the same copy of the CRT library only if both are linked with the same version of the CRT DLL. This could be a problem if you mix applications built with Visual C++ 5.0 with DLLs that are built by Visual C++ 4.1 or earlier. Because the DLL version of the CRT library used by Visual C++ 4.1 is msvcrt40.dll and the one used by Visual 5.0 is msvcrt.dll, you cannot build your application to use the same copy of the CRT library as these DLLs.

However, there is an exception. In US English version and some other localized versions of Windows NT 4.0 and Windows 2000, such as German, French, and Czech, a forwarder version of the msvcrt40.dll (version 4.20)is shipped. As a result, even though the DLL is linked with msvcrt40.dll and its user is linked with msvcrt.dll, you are still using the same copy of the CRT library because all calls made to msvcrt40.dll are forwarded to msvcrt.dll.

However this forwarder version of msvcrt40.dll is not available in Windows 95, Windows 98, Windows Millennium Edition (Me), and some localized versions of Windows NT 4.0 and Windows 2000, such as Japanese, Korean, and Chinese. So, if your application targets these operating systems, you need to either obtain an upgraded version of the DLL that doesn't rely on msvcrt40.dll or alter your application to not rely on using the same copy of the CRT libraries. If you have developed the DLL, this means rebuilding it with Visual C++ 4.2 or later. If it is a third- party DLL, you need to contact your vendor for an upgrade.

Please note that this forwarder DLL version of msvcrt40.dll (version 4.20) cannot be redistributed.

Example

This example passes a file handle across a DLL boundary.

The DLL and .exe file are built with /MD, so they share a single copy of the CRT.

If you rebuild with /MT so that they use separate copies of the CRT, running the resulting test1Main.exe results in an access violation.

// test1Dll.cpp
// compile with: /MD /LD
#include <stdio.h>
__declspec(dllexport) void writeFile(FILE *stream)
{
char s[] = "this is a string\n";
fprintf( stream, "%s", s );
fclose( stream );
}
// test1Main.cpp
// compile with: /MD test1dll.lib
#include <stdio.h>
#include <process.h>
void writeFile(FILE *stream);

int main(void)
{
FILE * stream;
errno_t err = fopen_s( &stream, "fprintf.out", "w" );
writeFile(stream);
system( "type fprintf.out" );
}

Output

this is a string

Example

This example passes environment variables across a DLL boundary.

// test2Dll.cpp
// compile with: /MT /LD
#include <stdio.h>
#include <stdlib.h>

__declspec(dllexport) void readEnv()
{
char *libvar;
size_t libvarsize;

/* Get the value of the MYLIB environment variable. */
_dupenv_s( &libvar, &libvarsize, "MYLIB" );

if( libvar != NULL )
printf( "New MYLIB variable is: %s\n", libvar);
else
printf( "MYLIB has not been set.\n");
free( libvar );
}
// test2Main.cpp
// compile with: /MT /link test2dll.lib
#include <stdlib.h>
#include <stdio.h>

void readEnv();

int main( void )
{
_putenv( "MYLIB=c:\mylib;c:\yourlib" );
readEnv();
}

Output

MYLIB has not been set.

If both the DLL and .exe file are built with /MD so that only one copy of the CRT is used, the program runs successfully and produces the following output:

New MYLIB variable is: c:\mylib;c:\yourlib


曾经的这一天...



评论 / 个人网页 / 扔小纸条
* 昵称

已经注册过? 请登录

新用户请先注册 以便能显示头像及追踪评论回复

Email
网址
* 评论
表情
 


 

分类小组论坛
杂谈 , 娱乐、八卦 , 文学、艺术 , 体育 , 旅游、同城 , 象牙塔 , 情感 , 时尚、生活 , 星座 , 科技

请注意遵守中华人民共和国法律法规, 如威胁到本站生存, 将依法向有关部门报告, 同时本站的相关记录可能成为对您不利的证据.

相关法律法规
全国人大常委会关于维护互联网安全的决定
中华人民共和国计算机信息系统安全保护条例
中华人民共和国计算机信息网络国际联网管理暂行规定
计算机信息网络国际联网安全保护管理办法
计算机信息系统国际联网保密管理规定