Python調(diào)用C++封裝DLL:模擬人工鼠標軌跡

在現(xiàn)代軟件開發(fā)中,鼠標軌跡模擬技術(shù)因其在自動化測試、游戲[腳本編寫等領(lǐng)域的廣泛應(yīng)用而備受青睞。本文將介紹如何使用C++語言將鼠標軌跡算法封裝為DLL(動態(tài)鏈接庫),以便在多種編程環(huán)境中實現(xiàn)高效調(diào)用,同時探討其在游戲行為檢測機制中的應(yīng)用。


4179dfdf531c44b69e9785b80b8e494b.png

一、鼠標軌跡算法概述

鼠標軌跡算法是一種模擬人類鼠標操作的程序,它能夠模擬出自然而真實的鼠標移動路徑。這種算法的底層實現(xiàn)通常采用C/C++語言,原因在于C/C++提供了高性能的執(zhí)行能力和直接訪問操作系統(tǒng)底層資源的能力。

通過將鼠標軌跡算法封裝為DLL,開發(fā)者可以輕松地在C++、Python、易語言、按鍵精靈等多種編程語言中調(diào)用這一技術(shù)。DLL封裝不僅簡化了集成過程,還增強了算法的跨語言兼容性。

二、跨語言兼容性

封裝為DLL的鼠標軌跡算法支持多種編程語言,包括但不限于C語言、C++、Python、易語言、按鍵精靈等。這種封裝方式為開發(fā)者提供了一個統(tǒng)一的接口,使得不同背景的開發(fā)者都能夠輕松地集成和使用鼠標軌跡模擬技術(shù)。

三、算法優(yōu)勢

封裝的鼠標軌跡算法具有以下優(yōu)勢:

模擬人工軌跡:算法能夠模擬出非貝塞爾曲線的自然鼠標移動,避免了機械式的直線移動。
適當?shù)耐nD/加速/減速:算法能夠根據(jù)需要模擬出鼠標的停頓、加速和減速,使得軌跡更加真實。
隨機軌跡:在固定兩點間,算法能夠生成不同的隨機軌跡,增加了軌跡的不可預(yù)測性。

四、應(yīng)用場景

鼠標軌跡算法在游戲自動化腳本、滑塊拖動軌跡等場景中有著廣泛的應(yīng)用。特別是在游戲行為檢測機制中,通過模擬正常玩家的鼠標操作,可以有效規(guī)避某些軟件或游戲的作弊檢測。

五、C/C++ 接口調(diào)用演示

以下是一個簡單的C/C++接口調(diào)用示例,展示了如何使用封裝的DLL進行鼠標軌跡模擬:

1.頭文件

/******************************************************************************************/
 
@SDK功能描述:C++鼠標軌跡
 
/******************************************************************************************/
 
#ifndef _SN_SDK_H__
 
#define _SN_SDK_H__
 
#include <windows.h>
 
//返回參數(shù)
 
typedef struct SN_RESULT {
 
int code; //錯誤碼,如果為 0 表示成功,否則表示錯誤號
 
char message[4096]; //錯誤信息,如果為 "OK" 表示成功,否則返回錯誤信息
 
}SN_RESULT;
 
//坐標參數(shù)
 
typedef struct SN_POINT
 
{
 
int x; //屏幕坐標,左上角(0,0),右下角(1920,1080 - 以實際屏幕為準)
 
int y; //屏幕坐標,左上角(0,0),右下角(1920,1080 - 以實際屏幕為準)
 
}SN_POINT;
 
//軌跡參數(shù)
 
typedef struct SN_POINT_PARAMS
 
{
 
struct SN_POINT point;//屏幕坐標,左上角(0,0),右下角(1920,1080 - 以實際屏幕為準)
 
int delayTime; //延時時間(單位:毫秒),僅供參考
 
}SN_POINT_PARAMS;
 
/*創(chuàng)建句柄
*
* 參數(shù):
* [in] szKey: 卡密(購買卡密:https://shop.4yuns.com/links/7C9F16B7)
* [out] pResult: 返回錯誤信息,參數(shù)pResult.code(錯誤碼)如果為 0 表示成功,否則表示錯誤號;
*
* 返回值:成功返回句柄,失敗返回NULL
*
*/
 
HANDLE WINAPI apiSNCreateHandle(char* szKey, SN_RESULT* pResult);
 
/*獲取鼠標移動軌跡
*
* 參數(shù):
* [in] handle: 句柄(通過調(diào)用apiSNCreateHandle得到)
* [in] startPoint: 開始坐標,左上角(0,0),右下角(1920,1080 - 以實際屏幕為準)
* [in] endPoint: 結(jié)束坐標,左上角(0,0),右下角(1920,1080 - 以實際屏幕為準)
* [out] points: 軌跡數(shù)組,如果數(shù)組中元素 point 出現(xiàn)(-1,-1),表示鼠標軌跡結(jié)束
*
* 返回值:返回參數(shù)SN_RESULT.code(錯誤碼)如果為 0 表示成功,否則表示錯誤號;
*
*/
 
SN_RESULT WINAPI apiSNMouseMove(HANDLE handle, SN_POINT startPoint, SN_POINT endPoint, SN_POINT_PARAMS* points);
 
/*獲取版本號
*
* 參數(shù):
* [in] handle: 句柄(通過調(diào)用apiSNCreateHandle得到)
* [out] szVersion: 版本號
*
* 返回值:返回參數(shù)SN_RESULT.code(錯誤碼)如果為 0 表示成功,否則表示錯誤號;
*
*/
 
SN_RESULT WINAPI apiSNGetVersion(HANDLE handle, char* szVersion);
 
/*獲取錯誤信息
*
* 參數(shù):
* [in] handle: 句柄(通過調(diào)用apiSNCreateHandle得到)
*
* 返回值:返回參數(shù)SN_RESULT.code(錯誤碼)如果為 0 表示成功,否則表示錯誤號;
*
*/
 
SN_RESULT WINAPI apiSNGetError(HANDLE handle);
 
/*釋放句柄(內(nèi)存)
*
* 參數(shù):
* [in] handle: 句柄(通過調(diào)用apiSNCreateHandle得到)
*
* 返回值:返回參數(shù)SN_RESULT.code(錯誤碼)如果為 0 表示成功,否則表示錯誤號;
*
*/
 
SN_RESULT WINAPI apiSNDestroyHandle(HANDLE handle);
 
#endif // !_SN_SDK_H__

2.Python代碼

'''
 
@SDK功能描述:鼠標軌跡
 
'''
 
import ctypes
import os
import sys
 
# 創(chuàng)建句柄
key = "SNKJuSrrrTnQ5UXYr4zr6XEveL7V2jg2X9h5BHGS5Des"  # 字符串
key_bytes = key.encode('utf-8')  # 將字符串轉(zhuǎn)換為 bytes
 
#設(shè)置模型文件路徑
onnx = "d://SNTrack.onnx"  # 字符串
onnx_bytes = onnx.encode('utf-8')  # 將字符串轉(zhuǎn)換為 bytes
 
# 假設(shè) DLL 文件名為 SNSDK.dll
sn_sdk = ctypes.WinDLL('d://SNSDK.dll')
 
# 定義 SN_RESULT 結(jié)構(gòu)體
class SN_RESULT(ctypes.Structure):
    _fields_ = [("code", ctypes.c_int),
                ("message", ctypes.c_char * 4096)]
 
# 定義 SN_POINT 結(jié)構(gòu)體
class SN_POINT(ctypes.Structure):
    _fields_ = [("x", ctypes.c_int),
                ("y", ctypes.c_int)]
 
# 定義 SN_POINT_PARAMS 結(jié)構(gòu)體
class SN_POINT_PARAMS(ctypes.Structure):
    _fields_ = [("point", SN_POINT),
                ("delayTime", ctypes.c_int)]
 
# 定義函數(shù)原型
sn_sdk.apiSNCreateHandle.argtypes = [ctypes.POINTER(ctypes.c_char),ctypes.POINTER(ctypes.c_char), ctypes.POINTER(SN_RESULT)]
sn_sdk.apiSNCreateHandle.restype = ctypes.c_void_p
 
sn_sdk.apiSNGetVersion.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_char)]
sn_sdk.apiSNGetVersion.restype = SN_RESULT
 
sn_sdk.apiSNMouseMove.argtypes = [ctypes.c_void_p, SN_POINT, SN_POINT, ctypes.POINTER(SN_POINT_PARAMS)]
sn_sdk.apiSNMouseMove.restype = SN_RESULT  # 根據(jù)實際情況調(diào)整
 
sn_sdk.apiSNDestroyHandle.argtypes = [ctypes.c_void_p]
sn_sdk.apiSNDestroyHandle.restype = SN_RESULT
 
 
 
 
result = SN_RESULT()  # 創(chuàng)建 SN_RESULT 實例
handle = sn_sdk.apiSNCreateHandle(key_bytes, onnx_bytes,ctypes.byref(result))
if result.code != 0:
    message = result.message.decode('gbk', errors='replace').strip()
    print("Result message:", message)
else:
    print("Handle created successfully")
 
# 獲取版本號
version = ctypes.create_string_buffer(4096)
version_result = sn_sdk.apiSNGetVersion(handle, version)
if version_result.code != 0:
    message = result.message.decode('gbk', errors='replace').strip()
    print("Result message:", message)
else:
    message = result.message.decode('gbk', errors='replace').strip()
    print("Result message:", version.value.decode())
 
# 獲取軌跡
# 定義開始和結(jié)束坐標
start_point = SN_POINT(100, 100)
end_point = SN_POINT(800, 800)
 
# 假設(shè)返回的軌跡點數(shù)量
num_points = 4096
 
# 創(chuàng)建一個數(shù)組來接收軌跡點
points_array = (SN_POINT_PARAMS * num_points)()
 
# 調(diào)用 apiSNMouseMove 函數(shù)
move_result = sn_sdk.apiSNMouseMove(handle, start_point, end_point, points_array)
 
# 檢查結(jié)果
if move_result.code != 0:
    message = result.message.decode('gbk', errors='replace').strip()
    print("Result message:", message)
else:
    # 遍歷并打印每個點
    for i in range(num_points):
        if points_array[i].point.x == -1 and points_array[i].point.y == -1:
            break  # 軌跡結(jié)束
        print(f"Point {i}: ({points_array[i].point.x}, {points_array[i].point.y},{points_array[i].delayTime})") # X坐標 ,Y坐標 ,延時時間
 
 
 
# 釋放句柄
destroy_result = sn_sdk.apiSNDestroyHandle(handle)
if destroy_result.code != 0:
    message = result.message.decode('gbk', errors='replace').strip()
    print("Result message:", message)
else:
    print("Handle destroyed successfully")
 
 
'''
輸出鼠標軌跡如下:
 
Handle created successfully
Result message: 1.0
Point 0: (100, 100,0)
Point 1: (100, 98,10)
Point 2: (103, 98,15)
Point 3: (111, 98,16)
Point 4: (116, 101,15)
Point 5: (122, 104,2)
Point 6: (129, 107,13)
Point 7: (135, 109,2)
Point 8: (144, 112,14)
Point 9: (155, 117,2)
Point 10: (167, 123,14)
Point 11: (180, 128,2)
Point 12: (193, 134,13)
Point 13: (209, 138,2)
Point 14: (225, 144,13)
Point 15: (238, 149,5)
Point 16: (254, 157,10)
Point 17: (269, 162,5)
Point 18: (282, 168,11)
Point 19: (298, 175,5)
Point 20: (311, 180,10)
Point 21: (326, 185,6)
Point 22: (341, 193,9)
Point 23: (369, 211,15)
Point 24: (396, 231,16)
Point 25: (419, 251,16)
Point 26: (442, 270,16)
Point 27: (461, 285,17)
Point 28: (481, 300,15)
Point 29: (491, 311,15)
Point 30: (502, 319,2)
Point 31: (513, 329,14)
Point 32: (523, 343,2)
Point 33: (535, 355,14)
Point 34: (546, 369,0)
Point 35: (558, 383,15)
Point 36: (570, 397,2)
Point 37: (582, 411,13)
Point 38: (596, 427,2)
Point 39: (608, 443,14)
Point 40: (620, 459,5)
Point 41: (633, 476,10)
Point 42: (645, 490,5)
Point 43: (656, 503,11)
Point 44: (666, 515,5)
Point 45: (675, 527,11)
Point 46: (684, 538,5)
Point 47: (694, 551,11)
Point 48: (702, 565,5)
Point 49: (710, 577,11)
Point 50: (716, 588,5)
Point 51: (723, 598,11)
Point 52: (728, 606,5)
Point 53: (733, 615,11)
Point 54: (738, 622,5)
Point 55: (743, 631,11)
Point 56: (747, 637,5)
Point 57: (750, 644,11)
Point 58: (753, 652,5)
Point 59: (756, 659,10)
Point 60: (759, 666,5)
Point 61: (761, 673,11)
Point 62: (764, 680,5)
Point 63: (766, 687,11)
Point 64: (768, 694,5)
Point 65: (769, 701,10)
Point 66: (771, 708,5)
Point 67: (772, 714,11)
Point 68: (773, 722,5)
Point 69: (774, 729,10)
Point 70: (777, 743,16)
Point 71: (778, 755,15)
Point 72: (778, 764,16)
Point 73: (780, 775,16)
Point 74: (781, 784,16)
Point 75: (781, 785,15)
Point 76: (781, 789,2)
Point 77: (781, 790,13)
Point 78: (781, 792,2)
Point 79: (782, 796,14)
Point 80: (782, 796,2)
Point 81: (782, 797,14)
Point 82: (782, 798,15)
Point 83: (782, 800,311)
Point 84: (784, 800,16)
Point 85: (784, 800,5)
Point 86: (785, 800,10)
Point 87: (786, 800,5)
Point 88: (786, 800,11)
Point 89: (788, 800,6)
Point 90: (789, 800,9)
Point 91: (790, 800,5)
Point 92: (791, 800,10)
Point 93: (793, 800,16)
Point 94: (795, 800,16)
Point 95: (796, 800,15)
Point 96: (797, 800,15)
Point 97: (797, 800,2)
Point 98: (798, 800,15)
Point 99: (798, 800,30)
Point 100: (799, 800,15)
Point 101: (799, 800,15)
Handle destroyed successfully
 
Process finished with exit code 0
 
'''

六、鼠標軌跡Demo效果演示

通過調(diào)用封裝的DLL接口,可以在不同的坐標點之間生成鼠標軌跡。例如:

從坐標(100,100)到(800,800),可以生成4條不同的鼠標軌跡。
從坐標(1000,100)到(800,800),可以生成2條不同的鼠標軌跡。


ce68be291e634be39e37bd1b5a114e6e.gif

七、獲取Demo

通過以下云盤鏈接下載Demo:

百度云盤下載
夸克云盤下載
123云盤下載

通過本文的介紹,我們可以看到,將鼠標軌跡算法封裝為DLL接口,不僅提高了算法的可移植性和易用性,還為游戲行為檢測等應(yīng)用場景提供了強大的技術(shù)支持。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容