//在APP InitInstance中写
/这段代码使得本地只能开一个程序实例。
hMutex = ::OpenMutex(MUTEX_ALL_ACCESS,TRUE,"KIPMutexName");
if(hMutex)
{
HWND hWnd=FindWindow(NULL,"KRISSER");
if(hWnd)
ShowWindow(hWnd,SW_RESTORE);
return FALSE;
}
hMutex = ::CreateMutex(NULL,TRUE,"KIPMutexName");
二、SOCKET环境初始化
//可在APP InitInstance中写
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
三、判断DLL是否已注册
//判断dll是否已注册。文件msxml4.dll是程序用到xml操作时必须包含的动态库
HKEY valueKey;
if(ERROR_SUCCESS!=RegOpenKeyEx(HKEY_CLASSES_ROOT,"CLSID//{00b7e0ab-817a-44ad-a04b-d1148d524136}",0,KEY_READ,&valueKey))
{
ExMessageBox("系统文件msxml4.dll未安装或者已丢失,重新安装可解决该问题!");//未注册成功
return FALSE;
}
四、DLL的动态导入
//.cpp中导出,与接口说明
HINSTANCE hInstance = ::LoadLibrary("CaptureVideoDLL.dll");//可以指明具体的路径,或者相对路径
if(hInstance != NULL)
{
startVideoCapture = (LStartVideoCapture) ::GetProcAddress(hInstance,"StartVideoCapture") ;
stopVideoCapture = (LStopVideoCapture) ::GetProcAddress(hInstance,"StopVideoCapture") ;
pauseLocalVideo = (LPauseLoaclVideo) ::GetProcAddress(hInstance,"PauseLoaclVideo") ;
runLocalVideo = (LRunLoaclVideo) ::GetProcAddress(hInstance,"RunLocalVideo") ;
moveVideoRect = (LMoveVideoRect) ::GetProcAddress(hInstance,"MoveVideoRect") ;
}
//.h 中做申明
LStartVideoCapture startVideoCapture ;
LStopVideoCapture stopVideoCapture ;
LPauseLoaclVideo pauseLocalVideo ;
LRunLoaclVideo runLocalVideo ;
LMoveVideoRect moveVideoRect ;
//-------------视频抓取--------------------------------
typedef HRESULT (WINAPI* LStartVideoCapture) (HWND hWnd,CRect videoRect,int nWidth2,int nHeight2) ;
typedef void (WINAPI* LStopVideoCapture) () ;
typedef void (WINAPI* LPauseLoaclVideo) () ;
typedef void (WINAPI* LRunLoaclVideo) () ;
typedef void (WINAPI* LMoveVideoRect) (CRect tmpRect2) ;
方法二:
1 .h中申明
void (*ff_avcodec_init)(void);
AVCodec *(*ff_avcodec_find_decoder)(enum CodecID id);
int (*ff_avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
int (*ff_avcodec_close)(AVCodecContext *avctx);
void (*ff_avcodec_register_all)(void);
AVCodecContext *(*ff_avcodec_alloc_context)(void);
int (*ff_avcodec_decode_video)(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, uint8_t *buf, int buf_size);
AVFrame *(*ff_avcodec_alloc_frame)(void);
2.cpp中导入
_hAvDll = LoadLibrary( "avcodec.dll" );
if ( _hAvDll == NULL )
return;
ff_avcodec_init = (void (__cdecl *)(void))
1GetProcAddress函数分为前后两个部份。
2如:ff_avcodec_init = (void (__cdecl *)(void))GetProcAddress(_hAvDll,"avcodec_init");
3
41 前一部分分别代表:
5(返回值类型)(标准C转换)(接口函数参数类型(可以多个))
62 后一部份分别代表:
7(导入的DLL实例指针,接口函数名称)
(_hAvDll,"avcodec_init");
ff_avcodec_find_decoder = (AVCodec *(__cdecl *)(enum CodecID))GetProcAddress(_hAvDll,"avcodec_find_decoder");
ff_avcodec_open = (int (__cdecl *)(struct AVCodecContext *,struct AVCodec *))GetProcAddress(_hAvDll,"avcodec_open");
ff_avcodec_close =(int (__cdecl *)(struct AVCodecContext *))GetProcAddress(_hAvDll,"avcodec_close");
ff_avcodec_register_all= (void (__cdecl *)(void)) GetProcAddress(_hAvDll,"avcodec_register_all");
ff_avcodec_alloc_context=(AVCodecContext *(__cdecl *)(void))GetProcAddress(_hAvDll,"avcodec_alloc_context");
ff_avcodec_decode_video=(int (__cdecl *)(struct AVCodecContext *,struct AVFrame *,int *,unsigned char *,int))GetProcAddress(_hAvDll,"avcodec_decode_video");
ff_avcodec_alloc_frame=(AVFrame *(__cdecl *)(void))GetProcAddress(_hAvDll,"avcodec_alloc_frame");
3 cpp中应用
ff_avcodec_init();
ff_avcodec_register_all();
_codec = ff_avcodec_find_decoder( CODEC_ID_H264 );
_context = ff_avcodec_alloc_context();
_picture= ff_avcodec_alloc_frame();
if ( _codec->capabilities&CODEC_CAP_TRUNCATED )
_context->flags |= CODEC_FLAG_TRUNCATED;
ff_avcodec_open( _context, _codec );