菜鸟求救:如何把一个函数转换成dll,比如下面这个函数?救急!

anmei 2003-11-25 03:17:02
声明:我不大会使vc,请您详细说说!
HRESULT SetAviVideoCompression(HAVI avi, HBITMAP hbm, AVICOMPRESSOPTIONS *opts, bool ShowDialog, HWND hparent)
{ if (avi==NULL) return AVIERR_BADHANDLE;
if (hbm==NULL) return AVIERR_BADPARAM;
DIBSECTION dibs; int sbm = GetObject(hbm,sizeof(dibs),&dibs);
if (sbm!=sizeof(DIBSECTION)) return AVIERR_BADPARAM;
TAviUtil *au = (TAviUtil*)avi;
if (au->iserr) return AVIERR_ERROR;
if (au->psCompressed!=0) return AVIERR_COMPRESSOR;
//
if (au->ps==0) // create the stream, if it wasn't there before
{ AVISTREAMINFO strhdr; ZeroMemory(&strhdr,sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = au->period;
strhdr.dwRate = 1000;
strhdr.dwSuggestedBufferSize = dibs.dsBmih.biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, dibs.dsBmih.biWidth, dibs.dsBmih.biHeight);
HRESULT hr=AVIFileCreateStream(au->pfile, &au->ps, &strhdr);
if (hr!=AVIERR_OK) {au->iserr=true; return hr;}
}
//
if (au->psCompressed==0) // set the compression, prompting dialog if necessary
{ AVICOMPRESSOPTIONS myopts; ZeroMemory(&myopts,sizeof(myopts));
AVICOMPRESSOPTIONS *aopts[1];
if (opts!=NULL) aopts[0]=opts; else aopts[0]=&myopts;
if (ShowDialog)
{ BOOL res = (BOOL)AVISaveOptions(hparent,0,1,&au->ps,aopts);
if (!res) {AVISaveOptionsFree(1,aopts); au->iserr=true; return AVIERR_USERABORT;}
}
HRESULT hr = AVIMakeCompressedStream(&au->psCompressed, au->ps, aopts[0], NULL);
AVISaveOptionsFree(1,aopts);
if (hr != AVIERR_OK) {au->iserr=true; return hr;}
DIBSECTION dibs; GetObject(hbm,sizeof(dibs),&dibs);
hr = AVIStreamSetFormat(au->psCompressed, 0, &dibs.dsBmih, dibs.dsBmih.biSize+dibs.dsBmih.biClrUsed*sizeof(RGBQUAD));
if (hr!=AVIERR_OK) {au->iserr=true; return hr;}
}
//
return AVIERR_OK;
}
...全文
121 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
COOL099 2003-11-26
  • 打赏
  • 举报
回复

#pragma comment(lib,"AVI_utils.lib")
anmei 2003-11-26
  • 打赏
  • 举报
回复
改为这样:

#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <vfw.h>
#include "avi_utils.h"


// First, we'll define the WAV file format.
#include <pshpack1.h>
typedef struct
{ char id[4]; //="fmt "
unsigned long size; //=16
short wFormatTag; //=WAVE_FORMAT_PCM=1
unsigned short wChannels; //=1 or 2 for mono or stereo
unsigned long dwSamplesPerSec; //=11025 or 22050 or 44100
unsigned long dwAvgBytesPerSec; //=wBlockAlign * dwSamplesPerSec
unsigned short wBlockAlign; //=wChannels * (wBitsPerSample==8?1:2)
unsigned short wBitsPerSample; //=8 or 16, for bits per sample
} FmtChunk;

typedef struct
{ char id[4]; //="data"
unsigned long size; //=datsize, size of the following array
unsigned char data[1]; //=the raw data goes here
} DataChunk;

typedef struct
{ char id[4]; //="RIFF"
unsigned long size; //=datsize+8+16+4
char type[4]; //="WAVE"
FmtChunk fmt;
DataChunk dat;
} WavChunk;
#include <poppack.h>




// This is the internal structure represented by the HAVI handle:
typedef struct
{ IAVIFile *pfile; // created by CreateAvi
WAVEFORMATEX wfx; // as given to CreateAvi (.nChanels=0 if none was given). Used when audio stream is first created.
int period; // specified in CreateAvi, used when the video stream is first created
IAVIStream *as; // audio stream, initialised when audio stream is first created
IAVIStream *ps, *psCompressed; // video stream, when first created
unsigned long nframe, nsamp; // which frame will be added next, which sample will be added next
bool iserr; // if true, then no function will do anything
} TAviUtil;




HRESULT WINAPI SetAviVideoCompression(HAVI avi, HBITMAP hbm, AVICOMPRESSOPTIONS *opts, bool ShowDialog, HWND hparent)
{ if (avi==NULL) return AVIERR_BADHANDLE;
if (hbm==NULL) return AVIERR_BADPARAM;
DIBSECTION dibs; int sbm = GetObject(hbm,sizeof(dibs),&dibs);
if (sbm!=sizeof(DIBSECTION)) return AVIERR_BADPARAM;
TAviUtil *au = (TAviUtil*)avi;
if (au->iserr) return AVIERR_ERROR;
if (au->psCompressed!=0) return AVIERR_COMPRESSOR;
//
if (au->ps==0) // create the stream, if it wasn't there before
{ AVISTREAMINFO strhdr; ZeroMemory(&strhdr,sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = au->period;
strhdr.dwRate = 1000;
strhdr.dwSuggestedBufferSize = dibs.dsBmih.biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, dibs.dsBmih.biWidth, dibs.dsBmih.biHeight);
HRESULT hr=AVIFileCreateStream(au->pfile, &au->ps, &strhdr);
if (hr!=AVIERR_OK) {au->iserr=true; return hr;}
}
//
if (au->psCompressed==0) // set the compression, prompting dialog if necessary
{ AVICOMPRESSOPTIONS myopts; ZeroMemory(&myopts,sizeof(myopts));
AVICOMPRESSOPTIONS *aopts[1];
if (opts!=NULL) aopts[0]=opts; else aopts[0]=&myopts;
if (ShowDialog)
{ BOOL res = (BOOL)AVISaveOptions(hparent,0,1,&au->ps,aopts);
if (!res) {AVISaveOptionsFree(1,aopts); au->iserr=true; return AVIERR_USERABORT;}
}
HRESULT hr = AVIMakeCompressedStream(&au->psCompressed, au->ps, aopts[0], NULL);
AVISaveOptionsFree(1,aopts);
if (hr != AVIERR_OK) {au->iserr=true; return hr;}
DIBSECTION dibs; GetObject(hbm,sizeof(dibs),&dibs);
hr = AVIStreamSetFormat(au->psCompressed, 0, &dibs.dsBmih, dibs.dsBmih.biSize+dibs.dsBmih.biClrUsed*sizeof(RGBQUAD));
if (hr!=AVIERR_OK) {au->iserr=true; return hr;}
}
//
return AVIERR_OK;
}


编译,提示:

--------------------Configuration: avi_utils - Win32 Debug--------------------
Linking...
Creating library Debug/avi_utils.lib and object Debug/avi_utils.exp
avi_utils.obj : error LNK2001: unresolved external symbol _AVIStreamSetFormat@16
avi_utils.obj : error LNK2001: unresolved external symbol _AVIMakeCompressedStream@16
avi_utils.obj : error LNK2001: unresolved external symbol _AVISaveOptionsFree@8
avi_utils.obj : error LNK2001: unresolved external symbol _AVISaveOptions@20
avi_utils.obj : error LNK2001: unresolved external symbol _AVIFileCreateStreamA@12
Debug/avi_utils.dll : fatal error LNK1120: 5 unresolved externals
Error executing link.exe.

avi_utils.dll - 6 error(s), 0 warning(s)

请问该如何解决?


止水之颠 2003-11-26
  • 打赏
  • 举报
回复
HRESULT WINAPI SetAviVideoCompression(HAVI avi, HBITMAP hbm, AVICOMPRESSOPTIONS *opts, bool ShowDialog, HWND hparent)
mail_dcb2 2003-11-26
  • 打赏
  • 举报
回复
oh, forgot, add __declspec(dllexport) to your functions declaration at yourclass.h
mail_dcb2 2003-11-26
  • 打赏
  • 举报
回复
a. setup a mfc dll (not extension) project
b. insert class in class view
c. add methods or properties
d. compile
e. see debug directory, copy lib and dll to your comsumer project directory
anmei 2003-11-26
  • 打赏
  • 举报
回复
re COOL099(Alan Zjou)
还没有AVI_utils.lib
那个文件是编译后才生成的
anmei 2003-11-25
  • 打赏
  • 举报
回复
re:函数定义成WINAPI形式
是否是这样:
WINAPI HRESULT SetAviVideoCompression(HAVI avi, HBITMAP hbm, AVICOMPRESSOPTIONS *opts, bool ShowDialog, HWND hparent)
可是编译无法通过
--------------------Configuration: av - Win32 Debug--------------------
Compiling...
av.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(56) : error C2146: syntax error : missing ';' before identifier 'SetAviVideoCompression'
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(56) : warning C4229: anachronism used : modifiers on data are ignored
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(56) : error C2501: 'HRESULT' : missing storage-class or type specifiers
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(56) : error C2377: 'HRESULT' : redefinition; typedef cannot be overloaded with any other symbol
d:\program files\microsoft visual studio\vc98\include\winnt.h(227) : see declaration of 'HRESULT'
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(56) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

av.dll - 4 error(s), 1 warning(s)


如果去掉HRESULT,提示
--------------------Configuration: av - Win32 Debug--------------------
Compiling...
av.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(57) : error C2556: 'int __stdcall SetAviVideoCompression(struct HAVI__ *,struct HBITMAP__ *,AVICOMPRESSOPTIONS *,bool,struct HWND__ *)' : overloaded function differs only by return type f
rom 'long __cdecl SetAviVideoCompression(struct HAVI__ *,struct HBITMAP__ *,AVICOMPRESSOPTIONS *,bool,struct HWND__ *)'
d:\program files\microsoft visual studio\myprojects\av\avi_utils.h(34) : see declaration of 'SetAviVideoCompression'
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(57) : error C2373: 'SetAviVideoCompression' : redefinition; different type modifiers
d:\program files\microsoft visual studio\myprojects\av\avi_utils.h(34) : see declaration of 'SetAviVideoCompression'
Error executing cl.exe.

av.dll - 2 error(s), 0 warning(s)

请问,该如何解决?
anmei 2003-11-25
  • 打赏
  • 举报
回复
只是想编译其中的一个函数 SetAviVideoCompression
头文件的声明也放在工程同名的xxx.cpp中吗
anmei 2003-11-25
  • 打赏
  • 举报
回复
编译无法通过:
--------------------Configuration: av - Win32 Debug--------------------
Compiling...
av.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(8) : error C2065: 'HAVI' : undeclared identifier
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(8) : error C2501: 'DECLARE_HANDLE' : missing storage-class or type specifiers
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(12) : error C2146: syntax error : missing ';' before identifier 'CreateAvi'
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(12) : error C2501: 'HAVI' : missing storage-class or type specifiers
D:\Program Files\Microsoft Visual Studio\MyProjects\av\av.cpp(12) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

源程序在
http://www.wischik.com/lu/programmer/avi_utils.html#examples
vcforever 2003-11-25
  • 打赏
  • 举报
回复
用AppWizard第一步中创建Win32 Dynamic-Link Library 然后下一步,创建一个空的工程,完成,这样就生成了一个空的win32dll工程,然后通过菜单file->new,在弹出对话框的file页选择c\c++ source file,在右边的file编辑框中输入和你的工程同名的xxx.cpp然后点OK,这样就把一个空白的.cpp文件添加到工程中了,然后把你的代码拷贝到空白文件中,包含相应的头文件,把你要在外部程序中用到的函数定义成WINAPI形式,最后在像添加.cpp文件一样。添加一个 .def文件,在.def文件中添加如下内容
EXPORTS
//在这里添加你要导出的函数的名称

编译你的工程!
anmei 2003-11-25
  • 打赏
  • 举报
回复
是用基于MFC的DLL的向导吗?我把代码直接加到.cpp的
// TODO: add construction code here,
// Place all significant initialization in InitInstance
处,然后在.def的
EXPORTS
; Explicit exports can go here
处添加SetAviVideoCompression
编译,提示有64个错误
能不能说说详细的步骤
dll是用来给其他语言调用的,delphi,是不是不能用mfc的dll向导啊?
xujunfeng008 2003-11-25
  • 打赏
  • 举报
回复
在vc的project里有一个dll工程,里面有基本的框架代码,把你的代码拷过去编译就可以了.

15,471

社区成员

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

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