[英]Using C++ code in Python with ctypes
I have the following C++ and both functions return int 0 which indicate a success. 我有以下C ++,并且两个函数都返回int 0,表示成功。
#include "stdafx.h"
#include <iostream>
#include "scapi.h"
int main() {
int ret;
ret = sc_LoginByLogon(L"hostname.domain.ltd", L"admin", L"pass");
std::cout << "LoginByLogon: " << ret << std::endl;
void* out_p;
ret = sc_GetUserInfoByUserLogon(L"username", &out_p);
std::cout << "GetUserInfoByUserLogon: " << ret << std::endl;
}
I'm now trying to create a wrapper in Python for these two functions and have come up with the following code: 我现在正尝试在Python中为这两个函数创建包装器,并提出了以下代码:
import sys
import ctypes
from ctypes import *
# Load DLL into memory.
class SUserInfoApiV4(Structure):
INTRFDEF_SIZE_RESERVED = 32
_fields_ = [
('m_nStructLength', c_int),
('m_nVersion', c_short),
('m_nSubVersion', c_short),
('m_achReserved', c_char * (INTRFDEF_SIZE_RESERVED - sizeof(c_long))),
('m_nAccessRights', c_long),
('m_nUserType', c_short),
('m_nUserId', c_int),
('m_nSubRights', c_long),
('m_nNotUsed1', c_int),
('m_wzFullName', c_wchar * 101),
('m_wzDescription', c_wchar * 101),
('m_wzEMail', c_wchar * 101),
('m_wzUserLogon', c_wchar * 21),
('m_wzPassword', c_wchar * 17),
('m_szCardNo', c_char * 40),
('m_szPINCode', c_char * 5),
('m_szPUKCode', c_char * 9),
('m_nLogonFails', c_short),
('m_bUserLocked', c_short),
('m_bUserDisabled', c_short),
('m_bAvoidPin', c_short),
('m_bPrintAll', c_short),
('m_bCardOpen', c_short),
('m_nBillingModel', c_short),
('m_nAccountingModel', c_short),
('m_nUserRights', c_int),
('m_bAllowEncryption', c_short),
('m_bAllowCheckPrinting', c_char),
('m_bAllowPmail', c_char),
('m_bDenyRetain', c_char),
('m_lCreationDate', c_long),
('m_lLastLogin', c_long),
('m_nServerId', c_int),
('m_nDomainId', c_int),
('m_nTreeNodeId', c_int),
('m_nNid', c_int),
('m_wzCostCode', c_wchar * 51)
]
def main(argv):
# import library
lib = ctypes.cdll.LoadLibrary('scAPI.dll')
# login
login = lib.sc_LoginByLogon('hostname.domain.ltd', 'admin', 'pass')
# sc_GetUserInfoByUserLogon
SUserInfoApiV4p = POINTER(SUserInfoApiV4)
sc_GetUserInfoByUserLogon = lib.sc_GetUserInfoByUserLogon
sc_GetUserInfoByUserLogon.argtypes = [c_wchar_p, POINTER(SUserInfoApiV4p)]
sc_GetUserInfoByUserLogon.restype = c_int
# get user object
out_p = SUserInfoApiV4p()
user = sc_GetUserInfoByUserLogon('username', byref(out_p))
# debug
print 'LoginByLogon: ' + str(login)
print 'GetUserInfoByUserLogon: ' + str(user)
#print 'Full name:', out_p.contents.m_wzFullName
if __name__ == '__main__':
main(sys.argv)
The code should have worked, but in Python only sc_LoginByLogon returns 0 while sc_GetUserInfoByUserLogon fails (1). 该代码应该可以正常工作,但是在Python中,只有sc_LoginByLogon返回0,而sc_GetUserInfoByUserLogon失败(1)。 The documentation only states that "1 = General failure", so I'm stuck.
该文档仅指出“ 1 =常规故障”,因此我陷入了困境。
Any obvious ideas would be highly appreciated. 任何明显的想法将不胜感激。
Thanks to Mark Tolonen it now works after explicitly defining the login function before calling. 多亏了Mark Tolonen,它现在可以在调用之前显式定义登录函数之后运行。 Didn't think I have to since it already returned int 0 which indicates a success.
没想到我必须这样做,因为它已经返回了表示成功的int 0。
The only problem is that all properties such as "m_wzFullName" skip the first character for some reason. 唯一的问题是,由于某些原因,所有属性(例如“ m_wzFullName”)都会跳过第一个字符。
Edit: I solved the last bug thanks to this thread: python ctypes pragma pack for byte aligned read 编辑:由于该线程,我解决了最后一个错误: python ctypes pragma pack用于字节对齐读取
# sc_LoginByLogon
sc_LoginByLogon = lib.sc_LoginByLogon
sc_LoginByLogon.argtypes = [c_wchar_p, c_wchar_p, c_wchar_p]
sc_LoginByLogon.restype = c_int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.