菜鸟求救:如何把一个函数转换成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;
}
...全文
221 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工程,里面有基本的框架代码,把你的代码拷过去编译就可以了.
内容概要:本文介绍了如何利用 GitHub Copilot 辅助进行程序调试与 Bug 分析,强调 Copilot 不仅可用于代码生成,更是强大的代码分析与调试工具。文章详细阐述了 Copilot 在调试复杂问题、老旧项目维护和难以复现 Bug 场景下的优势,提出了“先分析、再修改”的四步流程:分析原因→评估风险→提出方案→修改代码,并推荐结合错误日志、用户操作等信息精准提问,提升 AI 回答质量。同时展示了如何通过 Copilot 增强调试能力,如自动加日志、异常保护、生成测试数据和性能分析。最后通过游戏拾取系统的实际案例,说明如何结构化描述问题以获得有效反馈。; 适合人群:具备一定开发经验,正在参与项目调试或维护工作的程序员,尤其是面对复杂逻辑、历史代码或难复现 Bug 的 1-3 年开发者;也适合希望提升 AI 协作能力的技术人员。; 使用场景及目标:①快速定位偶发性崩溃、数据异常等问题根源;②理解无文档或结构混乱的老代码模块;③优化调试流程,借助 AI 生成诊断建议、修复方案与测试用例;④构建更具健壮性的程序,提前发现潜在缺陷。; 阅读建议:学习者应结合自身项目中的真实问题,按照文中提供的结构化提问模板实践,逐步训练与 Copilot 的协作能力,重视问题描述的完整性与准确性,避免直接要求修改代码,优先通过分析提升对系统的理解。
内容概要:本文针对高精度电流控制下的永磁同步电机(PMSM)参数辨识难题,提出一种基于粒子群优化算法(PSO)的多参数辨识模型,并在Simulink环境中完成系统级仿真实现。研究旨在克服传统控制中因电机参数(如定子电阻、交直轴电感、永磁磁链等)随温度、负载变化而失配所导致的电流控制性能下降问题。通过构建以电流跟踪误差为核心的适应度函数,利用PSO算法全局寻优能力强的特点,实现对关键电机参数的在线或离线精确辨识。文中详述了PSO算法的实现机制、参数初始化策略、收敛判据设计以及与PMSM矢量控制系统的集成方法,验证了该方案在不同运行工况下的辨识精度、收敛速度与鲁棒性,显著提升了电流环的动态响应品质与稳态控制精度。; 适合人群:具备电机驱动控制、现代控制理论及优化算法基础,熟悉MATLAB/Simulink仿真平台,从事高性能PMSM控制系统研发的研究生、高校科研人员及自动化、电力电子领域的工程师;特别适合正在开展参数自适应、智能控制算法应用等相关课题的研究者。; 使用场景及目标:①应用于高端制造装备、电动汽车驱动系统、精密伺服系统等对电流控制精度要求严苛的场合;②解决实际工程中因电机温升、老化等因素引发的参数漂移问题,提升系统长期运行稳定性;③作为智能优化算法与电机控制深度融合的教学案例,帮助理解PSO在复杂非线性系统参数辨识中的应用逻辑与实现路径。; 阅读建议:建议读者结合提供的Simulink仿真模型进行复现实验,重点剖析PSO算法模块与电机控制模型的接口设计、适应度函数的构建原则及参数敏感性分析方法,可进一步尝试引入其他先进优化算法(如GWO、HHO)进行性能对比,以深入掌握不同智能算法在工程辨识问题中的适用性与优劣。

15,465

社区成员

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

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