MFC对话框程序动态调用动态库,当更改了动态库项目中我看来无关痛痒的部分代码后会出现无法调用的问题

henzhuanxin 2018-04-09 05:34:39
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "DebugLog.h"

DebugLog x265Debug("videoFilter");

#if defined ( __cplusplus)
extern "C"
{
#include "x265.h"
};
#else
#include "x265.h"
#endif

int x265encode(AVFrame * picture) {
int i, j;
FILE *fp_src = NULL;
FILE *fp_dst = NULL;
int y_size;
int buff_size;
char *buff = NULL;
int ret;
x265_nal *pNals = NULL;
uint32_t iNal = 0;

x265_param* pParam = NULL;
x265_encoder* pHandle = NULL;
x265_picture *pPic_in = NULL;

//Encode 50 frame
//if set 0, encode all frame
int frame_num = 0;
int csp = X265_CSP_I420;
int width = 640, height = 360;

//fp_src = fopen("../cuc_ieschool_640x360_yuv420p.yuv", "rb");
//fp_src=fopen("../cuc_ieschool_640x360_yuv444p.yuv","rb");
fp_dst = fopen("D://file_store_dictionary//ds_test//ds//ds//cuc_ieschool.h265", "wb");
x265Debug.LogPrint(LOG_INFO, "the address of dst is %d \n", fp_dst);
//Check
if (fp_src == NULL || fp_dst == NULL)
{
return 0;
}


pParam = x265_param_alloc();
x265_param_default(pParam);
pParam->bRepeatHeaders = 1; //write sps,pps before keyframe
pParam->internalCsp = csp;
pParam->sourceWidth = width;
pParam->sourceHeight = height;
pParam->fpsNum = 25;
pParam->fpsDenom = 1;
//Init
pHandle = x265_encoder_open(pParam);
if (pHandle == NULL)
{
printf("x265_encoder_open err\n");
return 0;
}
y_size = pParam->sourceWidth * pParam->sourceHeight;

pPic_in = x265_picture_alloc();
x265_picture_init(pParam, pPic_in);
switch (csp) {
case X265_CSP_I444: {
buff = (char *)malloc(y_size * 3);
pPic_in->planes[0] = buff;
pPic_in->planes[1] = buff + y_size;
pPic_in->planes[2] = buff + y_size * 2;
pPic_in->stride[0] = width;
pPic_in->stride[1] = width;
pPic_in->stride[2] = width;
break;
}
case X265_CSP_I420: {
buff = (char *)malloc(y_size * 3 / 2);
pPic_in->planes[0] = buff;
pPic_in->planes[1] = buff + y_size;
pPic_in->planes[2] = buff + y_size * 5 / 4;
pPic_in->stride[0] = width;
pPic_in->stride[1] = width / 2;
pPic_in->stride[2] = width / 2;
break;
}
default: {
printf("Colorspace Not Support.\n");
return -1;
}
}

//detect frame number
if (frame_num == 0) {
//fseek(fp_src, 0, SEEK_END);
int deviator = 0;
deviator = (int)(picture->data[1]) - (int)(picture->data[0]);
switch (csp) {
case X265_CSP_I444:
{
frame_num = (deviator * 3) / (y_size * 3);
break;
}
case X265_CSP_I420:
{
frame_num = (deviator * 3 / 2) / (y_size * 3);
break;
}
default:
{
printf("Colorspace Not Support.\n");
return -1;
}
}
//fseek(fp_src, 0, SEEK_SET);
}

//Loop to Encode
for (i = 0; i<frame_num; i++)
{
switch (csp)
{
case X265_CSP_I444:
{
//fread(pPic_in->planes[0], 1, y_size, fp_src); //Y
//fread(pPic_in->planes[1], 1, y_size, fp_src); //U
//fread(pPic_in->planes[2], 1, y_size, fp_src); //V
memcpy(pPic_in->planes[0], picture->data[0], y_size);
memcpy(pPic_in->planes[1], picture->data[1], y_size);
memcpy(pPic_in->planes[2], picture->data[2], y_size);
break;
}
case X265_CSP_I420:
{
//fread(pPic_in->planes[0], 1, y_size, fp_src); //Y
//fread(pPic_in->planes[1], 1, y_size / 4, fp_src); //U
//fread(pPic_in->planes[2], 1, y_size / 4, fp_src); //V
memcpy(pPic_in->planes[0], picture->data[0], y_size);
memcpy(pPic_in->planes[1], picture->data[1], y_size/4);
memcpy(pPic_in->planes[2], picture->data[2], y_size/4);
break;
}
default:
{
printf("Colorspace Not Support.\n");
return -1; }
}

ret = x265_encoder_encode(pHandle, &pNals, &iNal, pPic_in, NULL);
printf("Succeed encode %5d frames\n", i);

for (j = 0; j<iNal; j++)
{
fwrite(pNals[j].payload, 1, pNals[j].sizeBytes, fp_dst);
}
}
//Flush Decoder
while (1)
{
ret = x265_encoder_encode(pHandle, &pNals, &iNal, NULL, NULL);
if (ret == 0)
{
break;
}
printf("Flush 1 frame.\n");
for (j = 0; j<iNal; j++)
{
fwrite(pNals[j].payload, 1, pNals[j].sizeBytes, fp_dst);
}
}
x265_encoder_close(pHandle);
x265_picture_free(pPic_in);
x265_param_free(pParam);
free(buff);
fclose(fp_src);
fclose(fp_dst);
return 0;
}
...全文
696 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_41833202 2018-04-10
  • 打赏
  • 举报
回复
将if (fp_src == NULL || fp_dst == NULL) { return 0; }修改为if (fp_dst == NULL) { return 0; },然后编译生成的动态库就不能被调用。
henzhuanxin 2018-04-10
  • 打赏
  • 举报
回复
引用 17 楼 schlafenhamster 的回复:
LoadLibrary(L"VideoDll.dll"); 使用全路径 试试!
最后发现原因在于本动态库调用了另一个动态库,而未能把那个动态库包含到MFC程序中。。。。。天呐
henzhuanxin 2018-04-10
  • 打赏
  • 举报
回复
引用 17 楼 schlafenhamster 的回复:
LoadLibrary(L"VideoDll.dll"); 使用全路径 试试!
还是不行,情况没有变化,看了MSDN说该方法只是让 load函数去特定路径下去找
schlafenhamster 2018-04-10
  • 打赏
  • 举报
回复
LoadLibrary(L"VideoDll.dll"); 使用全路径 试试!
赵4老师 2018-04-10
  • 打赏
  • 举报
回复
http://blog.csdn.net/zhao4zhong1/article/details/53078924 老司机找bug的十年心路历程
henzhuanxin 2018-04-10
  • 打赏
  • 举报
回复
引用 11 楼 schlafenhamster 的回复:
int x265encode(AVFrame * picture) 这么长的 函数 会 被 优化 掉 ? 通常 debug 是 不优化的 ! “现在的情况是原代码只有release模式编译出的dll才能调用“ release 才 优化
楼上指的是if判断语句那段会优化掉,不过事实证明release没有把它优化掉,代码就断在那里了
henzhuanxin 2018-04-10
  • 打赏
  • 举报
回复
引用 12 楼 schlafenhamster 的回复:
"结果是hMoudle为null " 即 HMODULE hMoudle = LoadLibrary(L"VideoDll.dll"); dll 不存在 ?
dll已经build并且放到MFC程序目录下了,但是LoadLibrary(L"VideoDll.dll")返回值为null
henzhuanxin 2018-04-10
  • 打赏
  • 举报
回复
引用 8 楼 zgl7903 的回复:
FILE *fp_src = NULL; if (fp_src == NULL || fp_dst == NULL) { return 0; } 代码应该是被编译器给优化掉了, 因为按上面的逻辑 你的函数始终返回0, 其它的代码都没有意义 if (/*fp_src == NULL || */ fp_dst == NULL) { return 0; }
没有被编译器优化掉,在release模式下我在这段代码后边加了log,并没有打印出来,说明确实代码断到这里了,所以这个肯定得改了
schlafenhamster 2018-04-10
  • 打赏
  • 举报
回复
"结果是hMoudle为null " 即 HMODULE hMoudle = LoadLibrary(L"VideoDll.dll"); dll 不存在 ?
henzhuanxin 2018-04-10
  • 打赏
  • 举报
回复
引用 8 楼 zgl7903 的回复:
FILE *fp_src = NULL; if (fp_src == NULL || fp_dst == NULL) { return 0; } 代码应该是被编译器给优化掉了, 因为按上面的逻辑 你的函数始终返回0, 其它的代码都没有意义 if (/*fp_src == NULL || */ fp_dst == NULL) { return 0; }
您提供了一个很重要的思路,可能编译器会将它优化掉确实,现在的情况是原代码只有release模式编译出的dll才能调用,而注释掉if判断语句内的fp_src == NULL后编译出的dll仍然不可用,无论是debug\release
henzhuanxin 2018-04-10
  • 打赏
  • 举报
回复
您提供了一个很重要的思路,可能编译器会将它优化掉确实,现在的情况是原代码只有release模式编译出的dll才能调用,而注释掉if判断语句内的fp_src == NULL后编译出的dll仍然不可用,无论是debug\release
schlafenhamster 2018-04-10
  • 打赏
  • 举报
回复
int x265encode(AVFrame * picture) 这么长的 函数 会 被 优化 掉 ? 通常 debug 是 不优化的 ! “现在的情况是原代码只有release模式编译出的dll才能调用“ release 才 优化
schlafenhamster 2018-04-10
  • 打赏
  • 举报
回复
“原因在于本动态库调用了另一个动态库” release 为什么 可以 ?
zgl7903 2018-04-09
  • 打赏
  • 举报
回复
FILE *fp_src = NULL; if (fp_src == NULL || fp_dst == NULL) { return 0; } 代码应该是被编译器给优化掉了, 因为按上面的逻辑 你的函数始终返回0, 其它的代码都没有意义 if (/*fp_src == NULL || */ fp_dst == NULL) { return 0; }
henzhuanxin 2018-04-09
  • 打赏
  • 举报
回复
引用 5 楼 schlafenhamster 的回复:
pFunlog =0 ?
和log没有关系,包含pFunlog 那段代码是为了说明我的调用方式是动态调用。修改的的确只是x265encode函数的红字部分,得到的结果是hMoudle为null,整个库项目别的地儿不动,就导致了build出的dll文件不能被调用
schlafenhamster 2018-04-09
  • 打赏
  • 举报
回复
pFunlog =0 ?
schlafenhamster 2018-04-09
  • 打赏
  • 举报
回复
改的 不是 int x265encode(AVFrame * picture) 吗 ? //fp_src = fopen("../cuc_ieschool_640x360_yuv420p.yuv", "rb"); 影响 log 文件 ?
henzhuanxin 2018-04-09
  • 打赏
  • 举报
回复
引用 3 楼 schlafenhamster 的回复:
"不能被调用" ?? 不是 有 log 文件 吗 ,看看 结果 !
CDialogEx::OnInitDialog(); HMODULE hMoudle = LoadLibrary(L"VideoDll.dll"); if (hMoudle == NULL) { ::MessageBox(NULL, L"VideoDll.dll文件不存在!", L"DLL文件加载失败", MB_OK); return -1; } PFUNLOG pFunlog = (PFUNLOG)GetProcAddress(hMoudle, "test_sunkaisens_icsp_video_pz_JNIVideoSend_VideoDllLogSetup"); pFunlog("D:\\file_store_dictionary\\ds_test\\MFCApplicationtest\\MFCApplicationtest\\MFCApplicationtest.log", 0xffffffff); 这是MFC程序调用动态库的程序片段,代码中的日志写在库内部的,现在调用是失败的
schlafenhamster 2018-04-09
  • 打赏
  • 举报
回复
"不能被调用" ?? 不是 有 log 文件 吗 ,看看 结果 !
henzhuanxin 2018-04-09
  • 打赏
  • 举报
回复
将if (fp_src == NULL || fp_dst == NULL) { return 0; }修改为if (fp_dst == NULL) { return 0; },然后编译生成的动态库就不能被调用,或者其他类似的改动都行不通,而该文档中其余代码的更改都不会影响动态库的调用
加载更多回复(1)

15,471

社区成员

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

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