我正在尝试用我的 python 应用程序进行统计、排行榜、成就等。 我知道我正确连接到了 dll,但似乎无论我如何尝试调用这些函数。我收到一条错误消息,说它们不存在。 如果有人知道如何在没有 ctypes 和库之类的情况下做到这一点。我对任何解决方案都持开放态度。
有人可以帮忙吗,非常感谢。
steam_api.SteamAPI_SteamUserStats.restype = ctypes.POINTER(ISteamUserStats) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 属性错误:找不到函数“SteamAPI_SteamUserStats”
import os
import platform
import ctypes
# Determine system architecture (32-bit or 64-bit)
is_64bits = platform.architecture()[0] == '64bit'
# Set path to the steam_api DLL based on system architecture
if is_64bits:
steam_api_path = os.path.abspath('./steamworks/redistributable_bin/win64/steam_api64.dll')
else:
steam_api_path = os.path.abspath('./steamworks/redistributable_bin/win32/steam_api.dll')
# Add directory containing steam_api DLL to PATH environment variable
os.environ['PATH'] = os.path.dirname(steam_api_path) + os.pathsep + os.environ.get('PATH', '')
# Load steam_api DLL
steam_api = ctypes.CDLL(steam_api_path)
# Define ISteamUserStats interface structure
class ISteamUserStats(ctypes.Structure):
_fields_ = []
steam_api.SteamAPI_InitFlat()
# Define function prototypes
steam_api.SteamAPI_SteamUserStats.restype = ctypes.POINTER(ISteamUserStats)
steam_api.SteamAPI_ISteamUserStats_RequestCurrentStats.argtypes = [ctypes.POINTER(ISteamUserStats)]
steam_api.SteamAPI_ISteamUserStats_RequestCurrentStats.restype = ctypes.c_bool
# Initialize Steam API
def steam_init():
if not steam_api.SteamAPI_InitFlat():
raise RuntimeError('Steam API Init failed')
# Shutdown Steam API
def steam_shutdown():
steam_api.SteamAPI_Shutdown()
# Request current stats function
def request_current_stats():
user_stats = steam_api.SteamAPI_SteamUserStats()
result = steam_api.SteamAPI_ISteamUserStats_RequestCurrentStats(user_stats)
if not result:
error_code = ctypes.get_last_error()
raise RuntimeError(f'RequestCurrentStats failed with error code: {error_code}')
# Example usage
if __name__ == '__main__':
try:
steam_init()
print("Steam API Initialized successfully.")
request_current_stats()
print("Current stats requested successfully.")
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
steam_shutdown()
print("Steam API Shutdown.")
steam_api_flat.h
包含以下内容:
// A versioned accessor is exported by the library
S_API ISteamUserStats *SteamAPI_SteamUserStats_v012();
// Inline, unversioned accessor to get the current version. Essentially the same as SteamUserStats(), but using this ensures that you are using a matching library.
inline ISteamUserStats *SteamAPI_SteamUserStats() { return SteamAPI_SteamUserStats_v012(); }
所以
SteamAPI_SteamUserStats
实际上并不是DLL的一部分。由于您没有使用 C 头文件,因此需要使用 SteamAPI_SteamUserStats_v012
来代替。