Skip to content

Commit

Permalink
Rename from ...W to GetModuleFileName and small fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd committed Jan 6, 2025
1 parent f2646df commit fae2869
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions comtypes/server/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
from comtypes.server import w_getopt
from comtypes.typeinfo import (
REGKIND_REGISTER,
GetModuleFileName,
LoadTypeLibEx,
UnRegisterTypeLib,
GetModuleFileNameW,
)

_debug = logging.getLogger(__name__).debug
Expand Down Expand Up @@ -225,7 +225,7 @@ def _get_serverdll():
"""Return the pathname of the dll hosting the COM object."""
handle = getattr(sys, "frozendllhandle", None)
if handle is not None:
return GetModuleFileNameW(handle, 260)
return GetModuleFileName(handle, 260)
import _ctypes

return _ctypes.__file__
Expand Down
2 changes: 1 addition & 1 deletion comtypes/test/test_server_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Test_get_serverdll(ut.TestCase):
def test_nonfrozen(self):
self.assertEqual(_ctypes.__file__, _get_serverdll())

@mock.patch.object(register, "GetModuleFileNameW")
@mock.patch.object(register, "GetModuleFileName")
@mock.patch.object(register, "sys")
def test_frozen(self, _sys, GetModuleFileName):
handle, dll_path = 1234, r"path\to\frozendll"
Expand Down
8 changes: 4 additions & 4 deletions comtypes/test/test_typeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from comtypes.typeinfo import (
TKIND_DISPATCH,
TKIND_INTERFACE,
GetModuleFileNameW,
GetModuleFileName,
LoadRegTypeLib,
LoadTypeLibEx,
QueryPathOfRegTypeLib,
Expand Down Expand Up @@ -94,16 +94,16 @@ def test_TypeInfo(self):
self.assertEqual(guid, ti.GetTypeAttr().guid)


class Test_GetModuleFileNameW(unittest.TestCase):
class Test_GetModuleFileName(unittest.TestCase):
def test_null_handler(self):
self.assertEqual(GetModuleFileNameW(None, 260), sys.executable)
self.assertEqual(GetModuleFileName(None, 260), sys.executable)

def test_loaded_module_handle(self):
import _ctypes

dll_path = _ctypes.__file__
hmodule = ctypes.WinDLL(dll_path)._handle
self.assertEqual(GetModuleFileNameW(hmodule, 260), dll_path)
self.assertEqual(GetModuleFileName(hmodule, 260), dll_path)


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions comtypes/typeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,15 +700,15 @@ def QueryPathOfRegTypeLib(
_GetModuleFileNameW.restype = DWORD


def GetModuleFileNameW(handle: Optional[int], maxsize: int) -> str:
def GetModuleFileName(handle: Optional[int], maxsize: int) -> str:
"""Returns the fullpath of the loaded module specified by the handle.
If the handle is NULL, returns the executable file path of the current process.
https://learn.microsoft.com/ja-jp/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryw
"""
buf = ctypes.create_unicode_buffer(maxsize)
length = _GetModuleFileNameW(handle, buf, ctypes.sizeof(buf))
return buf[:length] # type: ignore
length = _GetModuleFileNameW(handle, buf, maxsize)
return buf.value[:length]


################################################################
Expand Down

0 comments on commit fae2869

Please sign in to comment.