Jump to content
Korean Random
pipje2001

importing a .pyd file (written in C)

Recommended Posts

Is it possible to import a pyd file? i tried to import the file testC.pyd with this as code: (not actually a usefull code but just for testing)

#include <Python.h>

static PyObject *testCError;

static PyObject* testC_say_hello(PyObject* self, PyObject *args){
    const char* msg;
    int sts=0;

    if(!PyArg_ParseTuple(args, "s", &msg)){
        return NULL;
    }

    if(strcmp(msg, "this_is_an_error") == 0){
        PyErr_SetString(testCError, "This is a test exception");
        
        return NULL;
    }else{
        printf("This is C world\nYour message is: %s\n", msg);
        sts=21;
    }
    return Py_BuildValue("i", sts);
    
}

static PyMethodDef testC_methods[] = {
    {"say_hello", testC_say_hello, METH_VARARGS, "description"},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC inittestC(void){
    PyObject *m;
    m = Py_InitModule("testC", testC_methods);
    if(m == NULL) return;

    testCError = PyErr_NewException("testC.error", NULL, NULL);
    Py_INCREF(testCError);
    PyModule_AddObject(m, "error", testCError);
}

and i want to import this from mod_test.pyc:

import testC
status = testC.say_hello("morning")

both files are in C:\Games\World_of_Tanks_EU\res_mods\1.5.0.0\scripts\client\gui\mods

 

but when i try this it just gives me this error:

ERROR: [EXCEPTION] (scripts/client/gui/mods/__init__.py, 74):
Traceback (most recent call last):
  File "scripts/client/gui/mods/__init__.py", line 67, in _findValidMODs
  File "scripts/common/Lib/importlib/__init__.py", line 37, in import_module
  File "mod_test", line 2, in <module>
ImportError: No module named testC
 

i feel like i am doing something very simple wrong or it isn't even possible in world of tanks. Does anyone has an idea?

Share this post


Link to post

Short link
Share on other sites

0) Please do not use .pyd in WoT if you can to implement your mod in pure python.

1) AFAIK, you need to load .pyd via imp.load_dynamic() function
2) you need to built it against XFW.Native ( https://bitbucket.org/XVM/xfw.native/  ) 

Edited by Mixaill

Share this post


Link to post

Short link
Share on other sites
2 minutes ago, Mixaill said:

0) Please do not use .pyd in WoT if you can to implement your mod in pure python.

 

I wouldn't ask it if was possible in pure python :) , but why should I use it? 

Share this post


Link to post

Short link
Share on other sites

So, because WoT is statically linked with libpython, you do not have the option of using .pyd modules by default.

 

That's why:

a) you need to use pythonXY.dll wrapper for WoT (XFW.Native approach)

b) or you can find addresses for the Python/C api functions yourself ( memhelper approach koreanrandom.com/forum/topic/27531-/ )

Share this post


Link to post

Short link
Share on other sites

I see, I want to be as independent from other stuff as possible so I will try the second option I think, also why is it bad to use pyd? 

Share this post


Link to post

Short link
Share on other sites
Только что, pipje2001 сказал:

I see, I want to be as independent from other stuff as possible so I will try the second option I think, also why is it bad to use pyd? 

 

* your mod may be broken with a Worldoftanks.exe update (or it may be broken at each update, depending on the api search implementation)

* your mod may cause the game to crash

* your mod may be broken by user's AV software

* your mod can be broken forever when WG integrates client anticheat 

* possible problems with MacOS / Linux (incompatibility with CrossOver/Wine wrappers)

 

Share this post


Link to post

Short link
Share on other sites

I see why it's bad to use CD but what I am trying is just not possible in python so I don't have a choice :/

 

Is xfm only needed to build the file or also when using the mod? 

Share this post


Link to post

Short link
Share on other sites
13 часов назад, pipje2001 сказал:

Is xfm only needed to build the file or also when using the mod? 

You need to use headers from XFW.Native and wotmod which containts python27 wrapper dll.

 

devel package: com.modxvm.xfw.native_1.4.3.00085-devel.zip

redistributable wotmod for end users: com.modxvm.xfw.native_1.4.3.00085.wotmod

13 часов назад, pipje2001 сказал:

I see why it's bad to use CD but what I am trying is just not possible in python so I don't have a choice :/

 

You can also start executable via subprocess module and communicate with it via standard streams or named pipes.

Share this post


Link to post

Short link
Share on other sites

@MixaillThanks for the response, a wotmod is not such a big deal so i guess using xfm.native is fine. But is there some explanation about how to use it? i am not that experienced and i can't find an explanation anywhere

Share this post


Link to post

Short link
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×