Skip to content

Commit 9634497

Browse files
author
lixungeng
committed
avoid dead lock on quit
1 parent 58a6ac0 commit 9634497

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

c_sharp/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public static extern bool wechat_ocr(
1212
[MarshalAs(UnmanagedType.LPWStr)] string wechat_dir,
1313
[MarshalAs(UnmanagedType.LPStr)] string imgfn,
1414
SetResultDelegate set_res);
15+
[DllImport("wcocr.dll", CallingConvention = CallingConvention.Cdecl)]
16+
public static extern void stop_ocr();
1517

1618
// 定义委托类型
1719
public delegate void SetResultDelegate(IntPtr result);
@@ -45,5 +47,6 @@ static void Main(string[] args)
4547
SetResultDelegate setRes = new SetResultDelegate(stringResult.SetResult);
4648
bool success = wechat_ocr(args[0], args[1], args[2], setRes);
4749
Console.WriteLine($"OCR Success: {success} res:{stringResult.GetResult()}");
50+
stop_ocr();
4851
}
4952
}

java/Test.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ interface SetResCallback extends Callback {
1414
void callback(String arg);
1515
}
1616
boolean wechat_ocr(WString ocr_exe, WString wechat_dir, String imgfn, SetResCallback res);
17+
void stop_ocr();
1718
}
1819
public static void main(String [] args) {
1920
System.out.println("ocr begin...");
20-
String ocr_exe = "C:\\Users\\xungeng\\AppData\\Roaming\\Tencent\\WeChat\\XPlugin\\Plugins\\WeChatOCR\\7079\\extracted\\WeChatOCR.exe";
21-
String wechat_dir = "C:\\Program Files\\Tencent\\Weixin\\4.0.0.26";
21+
String ocr_exe = "C:\\Users\\xungeng\\AppData\\Roaming\\Tencent\\xwechat\\XPlugin\\plugins\\WeChatOcr\\8020\\extracted\\wxocr.dll";
22+
String wechat_dir = "C:\\Program Files\\Tencent\\Weixin\\4.0.2.13";
2223
String tstpng = "test.png";
2324
AtomicReference<String> result = new AtomicReference<>();
2425
WechatOCR.dll.wechat_ocr(new WString(ocr_exe), new WString(wechat_dir), tstpng, new WechatOCR.SetResCallback() {
@@ -28,5 +29,6 @@ public void callback(String args) {
2829
});
2930
System.out.println("OCR result: " + result.get());
3031
System.out.println("ocr end...");
32+
WechatOCR.dll.stop_ocr();
3133
}
3234
}

src/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ static string json_encode(const string& input) {
3939
return output;
4040
}
4141

42+
static std::unique_ptr<CWeChatOCR> g_instance;
43+
4244
extern "C" __declspec(dllexport)
4345
bool wechat_ocr(const wchar_t* ocr_exe, const wchar_t * wechat_dir, const char * imgfn, void(*set_res)(const char * dt))
4446
{
45-
static std::unique_ptr<CWeChatOCR> instance;
46-
if (!instance) {
47+
if (!g_instance) {
4748
auto ocr = std::make_unique<CWeChatOCR>(ocr_exe, wechat_dir);
4849
if (!ocr->wait_connection(5000)) {
4950
return false;
5051
}
51-
instance = std::move(ocr);
52+
g_instance = std::move(ocr);
5253
}
5354

5455
CWeChatOCR::result_t res;
55-
if (!instance->doOCR(imgfn, &res))
56+
if (!g_instance->doOCR(imgfn, &res))
5657
return false;
5758
string json;
5859
json += "{";
@@ -81,6 +82,11 @@ bool wechat_ocr(const wchar_t* ocr_exe, const wchar_t * wechat_dir, const char *
8182
return true;
8283
}
8384

85+
extern "C" __declspec(dllexport)
86+
void stop_ocr() {
87+
g_instance.reset();
88+
}
89+
8490
#ifdef _DEBUG
8591
// 定义这个函数仅方便使用regsvr32.exe调试本DLL, 使用环境变量WECHATOCR_EXE和WECHAT_DIR以及TEST_PNG传入调试参数
8692
extern "C" __declspec(dllexport)

0 commit comments

Comments
 (0)