0

I have the following problem. Any help would be appreciated :)

I am trying to call a C function from Python using ctypes. I have been successful in sharing the shared library (.dll on Windows with MS Visual Studio 2017) to Python 3.6.3.

There is a problem when I try to call the following function:

__declspec(dllexport) void printFunc()
{
    printf("hello world !!");
    //fflush(stdout);
}

I would like to see the output at Python interpreter as 'hello world !!' when I execute

mydll = cdll.LoadLibrary('path\\to\\sharedLibrary.dll')
mydll.printFunc.restype = None
mydll.printFunc()

Currently I see no output (because, the restype is None) when I execute the above code.

Expected output at Python interpreter after running the script:

>>> hello world !!

Any ideas please ??

11
  • 1
    You are probably using an IDE and stdout is redirected or replaced. Run it in a Windows console and you should see the output. Commented Nov 7, 2017 at 18:20
  • @Mark Tolonen, But I would like to see the output at python interpreter. Is that possible, and if yes, what changes should I make ? Commented Nov 8, 2017 at 8:29
  • 2
    Depends on what you are using. May not be possible with your IDE. Did you try running Python in a console? Commented Nov 8, 2017 at 8:39
  • How do you run Python? Also please show the entire Python script (what is cdll?) Commented Nov 8, 2017 at 9:49
  • @MarkTolonen, Yes I tried running Python script in a console and it worked. Thanks for your help !! But I am still interested to know if there is, anything exists, a possiblity to see the same output in python interpreter. Commented Nov 8, 2017 at 10:38

1 Answer 1

1
  • Your "hello world !!" should be printed in any case. Maybe stdout is redirected to somewhere, where you can't see it? Or line buffering is an issue, try fflush(stdout) after your printf() call.

  • The default return type for those functions is int. You do not explicitely return an int, so just some value which happened to be in some cpu register is taken as return value. Chances are, that it is the return value of the printf(), which is 14 in this case (the number of characters printed)

  • You can change the return type to void by issuing: mydll.printFunc.restype = None, then you shouldn't observe any integer as return value of the (python) function call.

  • If you want to have the output at your python interpreter instead of stdout, you will have to return the string from your function instead of passing it to printf() and adjust the return type accordingly:

.

 __declspec(dllexport) char *printFunc() {
     return "hello world !!";
 }

And in your python interpreter:

>>> from ctypes import *
>>> mydll = cdll.LoadLibrary('path\\to\\sharedLibrary.dll')
>>> mydll.printFunc.restype = c_char_p
>>> mydll.printFunc()
'hello world !!'
Sign up to request clarification or add additional context in comments.

7 Comments

Hello @Ctx, I have inserted 'fflush(stdout)' after printf() in C program and also, changed the mydll.printFunc.restype to None. Unfortunately, I still see the output as an integer i.e. 26. Any other suggestions please ?
@Vandith hm, can you c&p the actual session with inputs and outputs and edit it in your question above?
I am sorry; there was no output after the changes. The output 26 that I have mentioned was from other function.
@n.m. I have reduced the code to exactly what I have and the expected behaviour. Le t me know if this you have solution for this. Thanks!
@Vandith I have updated the answer, maybe this is what you want
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.