如何将一个文件封装到DLL里面,需要使用时从DLL里调出该文件?谢谢!

jsfun 2005-04-15 05:09:04
我现在有一个后缀名为.OUT的文件,这是DSP加载程序类型文件,我现在需要把这个文件封装到我写的DLL程序里面,在程序需要用到该文件时又将其从DLL里面调出,请问怎么做?简单的说就是如何把一个文件封装到DLL中,在需要使用该文件时又从DLL中将其调出!请给出详细的解答或者给出相关网站的网址我自己看!谢谢!
...全文
288 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cutecute 2005-04-21
  • 打赏
  • 举报
回复
用资源就可以了.
oyljerry 2005-04-20
  • 打赏
  • 举报
回复
把文件作为资源放进去
jsfun 2005-04-20
  • 打赏
  • 举报
回复
没人做过这个一样的东西吗?可否发段代码给我?谢谢!
zhangjinlin218 2005-04-20
  • 打赏
  • 举报
回复
up
柯本 2005-04-20
  • 打赏
  • 举报
回复
还没解决?
我的方法再细化一下
如有一个文件t.out 为了简化说明,只写一个32字节的:
43 83 FB 40 75 03 E8 D0-00 FE C9 75 EE B8 0D 0A
89 87 70 92 43 43 E8 C0-00 C3 33 DB 80 7C 06 00

先写一个代码生成程序:(我用标准的C写了一个)

#include <stdio.h>
main()
{
FILE *fin,*fout;
char c;
int i=0;

fin=fopen("t.out","rb");
fout=fopen("temp.h","w");
c=getc(fin);
i=2;
fprintf(fout,"unsigned char data[]={\n\t 0x%02x",(unsigned char)c);
while((c=getc(fin))!=EOF)
{
fprintf(fout,",0x%02x",(unsigned char)c);
if(i++%8==0)
fprintf(fout,"\n\t");
}
fprintf(fout,"\n};\n");

fclose(fin);
fclose(fout);

}


编译运行,将生成以下程序temp.h
unsigned char data[]={
0x43,0x83,0xfb,0x40,0x75,0x03,0xe8,0xd0
,0x00,0xfe,0xc9,0x75,0xee,0xb8,0x0d,0x0a
,0x89,0x87,0x70,0x92,0x43,0x43,0xe8,0xc0
,0x00,0xc3,0x33,0xdb,0x80,0x7c,0x06,0x00

};


最后在你的dll程序中加入
#include "temp.h"

....
....
//你的导出函数中加
...
FILE *fp;
fp=fopen("t.out","wb");
fwrite(data,1,sizeof(data),fp);
fclose(fp);
...

就可以了
NOMADBLUE 2005-04-20
  • 打赏
  • 举报
回复
还是参考一下加壳程序的做法吧
jsfun 2005-04-20
  • 打赏
  • 举报
回复
问题是我在需要这个文件时,如何将资源读出来存为文件,关键是我的资源不是标准资源,是任意类型的文件!请问怎么做?最好有点代码!
jsfun 2005-04-18
  • 打赏
  • 举报
回复
自己顶一下!
柯本 2005-04-15
  • 打赏
  • 举报
回复
如果文件不是很大(我想DSP的OUT文件应该是K级的吧),有一简单方法
可以考虑用外部变量
unsigned char data[]={ 0xda,0xdd,... };
当然,先要编一程序将.out生成如 0xda,0xdd,...这样的格式,然后贴在你的程序中
使用时很便地将data写入一文件即可
---------------------------------------
我从老外那里学的,以前我也是这样做的
kugou123 2005-04-15
  • 打赏
  • 举报
回复
把文件导入到资源,然后在DLL中把资源释放,还原成你的文件。

看看这个例子,和你的要求相似

http://www.xiaozhou.net/cooldog/blogview.asp?logID=71
jsfun 2005-04-15
  • 打赏
  • 举报
回复
根据我的要求没有好一点详细一点的例子吗?
老夏Max 2005-04-15
  • 打赏
  • 举报
回复
参考:
http://www.vckbase.com/vckbase/vckbase12/vc/nonctrls/misc_21/1221002.htm
NOMADBLUE 2005-04-15
  • 打赏
  • 举报
回复
这涉及到了文件的打包问题,思路:
1,将此文件创建成资源,保存在你的DLL文件中,使用的时候从DLL文件中提取
2,定义一个变量数组,大小为你的文件,然后将文件COPY进去,使用的时候,创建一个文件,并将数组内容拷到文件中
  • 打赏
  • 举报
回复
把这个文件作为dll工程的资源编译,使用时将资源还原成文件,:
LoadResource
FindResource
LockResource


Updating Resources
The following example copies a dialog box resource from one executable file, Hand.exe, to another, Foot.exe, by following these steps:

Use the LoadLibrary function to load the executable file Hand.exe.
Use the FindResource and LoadResource functions to locate and load the dialog box resource.
Use the LockResource function to retrieve a pointer to the dialog box resource data.
Use the BeginUpdateResource function to open an update handle to Foot.exe.
Use the UpdateResource function to copy the dialog box resource from Hand.exe to Foot.exe.
Use the EndUpdateResource function to complete the update.
The following code implements these steps.

HRSRC hResLoad; // handle to loaded resource
HANDLE hExe; // handle to existing .EXE file
HRSRC hRes; // handle/ptr. to res. info. in hExe
HANDLE hUpdateRes; // update resource handle
char *lpResLock; // pointer to resource data
BOOL result;
// Load the .EXE file that contains the dialog box you want to copy.
hExe = LoadLibrary("hand.exe");
if (hExe == NULL)
{
ErrorHandler("Could not load exe.");
}

// Locate the dialog box resource in the .EXE file.
hRes = FindResource(hExe, "AboutBox", RT_DIALOG);
if (hRes == NULL)
{
ErrorHandler("Could not locate dialog box.");
}

// Load the dialog box into global memory.
hResLoad = LoadResource(hExe, hRes);
if (hResLoad == NULL)
{
ErrorHandler("Could not load dialog box.");
}

// Lock the dialog box into global memory.
lpResLock = LockResource(hResLoad);
if (lpResLock == NULL)
{
ErrorHandler("Could not lock dialog box.");
}

// Open the file to which you want to add the dialog box resource.
hUpdateRes = BeginUpdateResource("foot.exe", FALSE);
if (hUpdateRes == NULL)
{
ErrorHandler("Could not open file for writing.");
}

// Add the dialog box resource to the update list.
result = UpdateResource(hUpdateRes, // update resource handle
RT_DIALOG, // change dialog box resource
"AboutBox", // dialog box name
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), // neutral language
lpResLock, // ptr to resource info
SizeofResource(hExe, hRes)); // size of resource info.
if (result == FALSE)
{
ErrorHandler("Could not add resource.");
}

// Write changes to FOOT.EXE and then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
ErrorHandler("Could not write changes to file.");
}

// Clean up.
if (!FreeLibrary(hExe))
{
ErrorHandler("Could not free executable.");
}

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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