我有以下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;
}
我现在正在尝试使用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)
代码应该有效,但在Python中,只有sc_LoginByLogon返回0而sc_GetUserInfoByUserLogon失败(1)。 The documentation仅表示“1 =一般失败”,所以我被卡住了。
任何明显的想法都会受到高度赞赏。
答案 0 :(得分:0)
感谢Mark Tolonen,它现在可以在调用之前显式定义登录函数。我没想过,因为它已经返回0表示成功。
唯一的问题是所有属性,例如&#34; m_wzFullName&#34;因某种原因跳过第一个角色。
修改强> 由于这个线程,我解决了最后一个bug: python ctypes pragma pack for byte aligned read
# sc_LoginByLogon
sc_LoginByLogon = lib.sc_LoginByLogon
sc_LoginByLogon.argtypes = [c_wchar_p, c_wchar_p, c_wchar_p]
sc_LoginByLogon.restype = c_int