基本上我只从这个 github 存储库中提取了 avscan 样本avscan,我想重新定义/复制并粘贴用户模式主要功能中的代码,并在我的 Cython 代码中实现它,如下所示。
cythonmod.pyx:
cdef extern from *:
r"""
#include <windows.h>
#include <stdio.h>
#include <fltUser.h>
#include "..\\avscan\\user\\utility.h"
#include "..\\avscan\\inc\\avlib.h"
#include "..\\avscan\\user\\userscan.h"
HRESULT UserScanInit(PUSER_SCAN_CONTEXT pContext);
int usermodeDef(){
printf("User mode connecting to kernel mode\n");
UCHAR c;
HRESULT hr = S_OK;
USER_SCAN_CONTEXT userScanCtx = {0};
hr = UserScanInit(&userScanCtx);
if (FAILED(hr)) {
fprintf(stderr, "Failed to initialize user scan data\n");
DisplayError( hr );
return 255;
}
//
// Read user's input until it reads 'q'
//
for(;;) {
printf("press 'q' to quit: ");
c = (unsigned char) getchar();
if (c == 'q') {
break;
}
}
//
// Finalize the scan thread contexts.
//
hr = UserScanFinalize(&userScanCtx);
if (FAILED(hr)) {
fprintf(stderr, "Failed to finalize the user scan data.\n");
}
return 0;
}
"""
int usermodeDef()
setup.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
ext_modules = [
Extension(
"cythonmod",
sources=["cythonmod.pyx"],
include_dirs=["../avscan/inc", "../avscan/user"],
extra_compile_args=["-include", "../avscan/user/userscan.h"],
),
]
setup(
name="avscanner",
ext_modules=cythonize(ext_modules),
compiler_directives={'language_level': 3, "unraisable_tracebacks": True}
)
当我在这里运行 setup.py 时,我得到了
UserScanInit
未定义的错误。
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build\temp.win-amd64-3.9\Release\commonmodules.o:commonmodules.c:(.text+0x75df5): undefin
ed reference to `UserScanInit'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build\temp.win-amd64-3.9\Release\commonmodules.o:commonmodules.c:(.text+0x75e1d): undefin
ed reference to `UserScanFinalize'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build\temp.win-amd64-3.9\Release\commonmodules.o:commonmodules.c:(.text+0x75e5b): undefin
ed reference to `DisplayError'
collect2.exe: error: ld returned 1 exit status
error: command 'C:\\MinGW\\bin\\gcc.exe' failed with exit code 1
setup.py 生成的 GCC 选项:
gcc -mdll -O -Wall -I../avscan/inc -I../avscan/user -IC:\Users\username\AppData\Local\Programs\Python\Python39\include -IC:\Users\username\AppData\Local\Programs\Python\Python39\include -c cythonmod.c -o build\temp.win-amd64-3.9\Release\cythonmod.o -include ../avscan/user/userscan.h
我尝试了很多其他的东西,即从
cdef extern
直接导入 userscan.h 作为 "../avscan/user/userscan.h"
:但仍然无法修复它。
谁能告诉我如何将外部 C 代码导入 cython 的内联 C?