//#include "widget.h"
#include <QApplication>
#include<QDebug>
#include<QLibrary>
#include<string>
#include<QString>
#include<windows.h>
using std::string;
#include<QProcess>
#include<QStringList>
//typedef std::string ( *shibei)(string, string ,string ,string ,string); //定義函式指標
//typedef QStringList(*FUN1)(QString dll_Name);
typedef string (CALLBACK* p_shibie)(string model_file, string trained_file, string mean_file,
string label_file, string image_file);
//typedef bool (CALLBACK* SetKeyBoardHook)(HWND);
HINSTANCE hDLL; // Handle to DLL
p_shibie shibiep; // Function pointer
string str;
//string caffe_exe_path= "classification.exe";
//path +
string path = "D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/";
string caffe_path = path + "Myclassification.dll";
string deploy_path= path + "d.p";
string model_path= path + "r.m";
string mean_path= path + "t.b";
string words_path= path + "w.t";
string image_path = path + "1.png";
void test2()
{
//D:/Qfile/testDll/
QLibrary mylib("Myclassification.dll");
if(mylib.load())
{
qDebug() << "dllhook加載成功";
shibiep = (p_shibie)mylib.resolve("shibie");
if(shibiep)
{
qDebug() << "SetKeyBoardHook加載成功";
str = shibiep(deploy_path, model_path, mean_path, words_path, image_path);
qDebug()<<"result:"<<QString::fromStdString(str);
//bool flag = open((HWND)this->winId());
}
}
}
void test()
{
hDLL = LoadLibrary("Myclassification");//"dllhook"為DLL的檔案名
if(hDLL != NULL)
{
qDebug() << "dllhook加載成功";
//"SetKeyBoardHook"為動態庫里實作的函式名稱
shibiep = (p_shibie)GetProcAddress(hDLL, "shibie");
if(shibiep != NULL)
{
qDebug() << "SetKeyBoardHook Success";
str = (shibiep)(deploy_path, model_path, mean_path, words_path, image_path);
qDebug()<<"result:"<<QString::fromStdString(str);
}
else
{
//FreeLibrary(hDLL);
qDebug()<<"SetKeyBoardHook Fail";
return;
}
}
}
int main(int argc, char *argv[])
{
// QApplication a(argc, argv);
test2();
// test();
// Widget w;
// w.show();
// return a.exec();
}
一直報錯,路徑換成絕對的和相對都不行。
錯誤提示:
dllhook加載成功
SetKeyBoardHook加載成功
*** Check failure stack trace: ***
21:45:57: 程式例外結束。
請教各位大佬如何解決問題?
dll為caffe的執行函式
.pro檔案為:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES-= UNICODE
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#LIBS += -L. -lExcelApI
#LIBS += -L. C:/Users/Admin/Downloads/QTDLL/QTDLL/libcaffe/ \
# -Myclassification \
# -gflags
#LIBS += -L. -lMyclassification
INCLUDEPATH += D:/Desktop/OpenCV/64/include
LIBS += D:/Desktop/OpenCV/64/mingw/lib/libopencv_*.a
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/libcaffe/ -lMyclassification
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/libcaffe/ -lMyclassificationd
INCLUDEPATH += $$PWD/libcaffe
DEPENDPATH += $$PWD/libcaffe
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/gflags.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_cpp.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_f90cstub.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_fortran.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_hl.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_hl_cpp.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_hl_f90cstub.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_hl_fortran.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/hdf5_tools.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/libgcc_s_sjlj-1.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/libgflags.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/libgfortran-3.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/libglog.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/libopenblas.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/libquadmath-0.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/lmdb.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_calib3d2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_contrib2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_core2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_features2d2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_ffmpeg2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_flann2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_gpu2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_highgui2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_imgproc2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_legacy2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_ml2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_nonfree2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_objdetect2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_ocl2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_photo2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_stitching2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_superres2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_video2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/opencv_videostab2410.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/szip.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/zlib.dll
LIBS += D:/Qfile/build-testDll-Desktop_Qt_5_14_2_MinGW_32_bit-Release/release/lmdb.dll
#win32:CONFIG(release, debug|release): LIBS += -L$$PWD/libcaffe/ -llmdb
#else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/libcaffe/ -llmdbd
#INCLUDEPATH += $$PWD/libcaffe
#DEPENDPATH += $$PWD/libcaffe
謝謝 大佬
uj5u.com熱心網友回復:
dllhook加載成功SetKeyBoardHook加載成功
*** Check failure stack trace: ***
21:45:57: 程式例外結束。
1、由輸出可以知道是
str = shibiep(deploy_path, model_path, mean_path, words_path, image_path);
這一句報的錯誤。
2、排查點,一個是確認入參是否都ok?? 另一個就得跟進dll 里面具體分析了
uj5u.com熱心網友回復:
我覺得你絕對路徑都有問題的話,就應該去檢查一下你的匹配情況了,是否32位,64位不匹配,是否minW和visual studio版本不匹配。用mingW編譯的和用vs編譯的混用有問題。64位去呼叫32位的dll也會有問題uj5u.com熱心網友回復:
32位和64位都編譯了試過不行,但是java代碼運行了可以成功呼叫。路徑換了/、\\、\\\\都還是不行。
主要是沒有dll的源代碼,因此不能追進去調式。
uj5u.com熱心網友回復:
package com.cizon.tec.serve.impl;
import com.cizon.tec.serve.CaffeLoader;
import com.cizon.tec.serve.DistinguishServe;
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class CaffeServeImpl implements DistinguishServe {
String caffe_exe_path= "classification.exe";
String caffe_path="Myclassification";
String deploy_path= "d.p";
String model_path= "r.m";
String mean_path= "t.b";
String words_path= "w.t";
classficationdll Instance=null;
public CaffeServeImpl(String path){
caffe_exe_path=path+File.separator+caffe_exe_path;
caffe_path=path+File.separator+caffe_path;
deploy_path=path+File.separator+deploy_path;
model_path=path+File.separator+model_path;
mean_path=path+File.separator+mean_path;
words_path=path+File.separator+words_path;
System.out.println(caffe_path);
Instance = (classficationdll) Native.loadLibrary(caffe_path, classficationdll.class);
}
public CaffeServeImpl(){
try {
String path= CaffeLoader.initialize();
System.out.println(path);
path=new File(path).getAbsolutePath();
caffe_exe_path=path+File.separator+caffe_exe_path;
caffe_path=path+File.separator+caffe_path;
deploy_path=path+File.separator+deploy_path;
model_path=path+File.separator+model_path;
mean_path=path+File.separator+mean_path;
words_path=path+File.separator+words_path;
System.out.println("++++++++++++++++++++++"+caffe_path);
Instance = (classficationdll) Native.loadLibrary(caffe_path, classficationdll.class);
System.out.println("OKOK");
} catch (Exception e) {
e.printStackTrace();
}
}
public String exeCmd(String commandStr) {
BufferedReader br = null;
try {
commandStr=caffe_exe_path+" "+deploy_path+" "+model_path+" "+mean_path+" "+words_path+" "+commandStr;
System.out.println(commandStr);
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream(),Charset.forName(System.getProperty("file.encoding"))));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
finally
{
if (br != null)
{
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "";
}
public interface classficationdll extends Library {
String shibie(String model_file, String trained_file, String mean_file, String label_file, String image_file);
}
@Override
public String distinguish(String path) {
System.setProperty("jna.encoding","GBK");
//return exeCmd(path);
return Instance.shibie(deploy_path,model_path,mean_path,words_path,path);
}
}
這是java呼叫
uj5u.com熱心網友回復:
你確定你函式的引數是對的?java的string照理對應的C的引數是const char*而不是std::string
uj5u.com熱心網友回復:
之前,遇到引數是公共型別,比如是 CString、std::string 的,我就很怕,所以一直避免.因為不同版本的 VC運行時,std::string 可能不同,debug /release 編譯出來的同樣是二進制邊界不同。(也許有相同的,但我沒精力一個一個證明或者證偽,所以極力避開,都用 char* 傳入,或者自寫的類物件指標用來帶回字串或者復雜型別 , 還有就是 統一所有開發環境)
而你要作 hook ,那可能那個東西不知道誰、用啥寫的,以及怎么編譯的。。。。
說白了,以這個函式原型,用 VS2010撰寫的dll ,同樣要用 VS2010撰寫的.exe/.dll 去呼叫。debug撰寫的dll,也要用debug撰寫的.exe/.dll去呼叫。這樣肯定沒問題,這樣出問題也是其他原因導致的。
如果沒這個問題,就當我路過。。。
uj5u.com熱心網友回復:
感覺各位的回復,最后我還是放棄了直接使用C++呼叫dll,在qt中可以使用qml呼叫java方法轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/7729.html
標籤:Qt
上一篇:Android如何實作多級通訊樹
下一篇:Android 仿今日頭條
