0

So I am basically new to python ctypes and I have little or no knowledge on C programming or any none python language...but I have a solid understanding of the dll protocol and working...so I made this call into a dll file to access a printf and then use it....my result came out oddly and not as expected....can anyone help explain what is happening here ...here's my code

From ctypes import *

msvcrt = cdll.msvcrt
message_string = "Hello World! \n"
msvcrt.printf("Testing: %s", message_string)

And here is my result

1
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Brian Obot
  • 336
  • 4
  • 16

2 Answers2

0

The reason is that you must pass a bytes string (b"whatever") not a regular python literal string.

https://stackoverflow.com/a/6273618/3870025

From ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello World! \n"
msvcrt.printf(b"Testing: %s", message_string)
zvi
  • 3,677
  • 2
  • 30
  • 48
0
from ctypes import *
msvcrt = cdll.msvcrt

message_string = b"Hello World!\n"
msvcrt.printf(b"Testing: %s", message_string)

Note that printf prints to the real standard output channel, not to sys.stdout, so these examples will only work at the console prompt, not from within IDLE or PythonWin:

Jonas
  • 41
  • 8