1

I'm working on Windows with Python 3.6. I'm trying to make a wWinMain() GUI application that uses an embedded python interpreter. I'm having various issues with being unable to load extension modules, but I won't go into that now because I've tracked my issue down to a much simpler test case.

To begin, consider the source code of pythonw.exe:

/* Minimal main program -- everything is loaded from the library. */

#include "Python.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int WINAPI wWinMain(
    HINSTANCE hInstance,      /* handle to current instance */
    HINSTANCE hPrevInstance,  /* handle to previous instance */
    LPWSTR lpCmdLine,         /* pointer to command line */
    int nCmdShow              /* show state of window */
)
{
    return Py_Main(__argc, __wargv);
}

Now consider a simple python script, which I save in source.py

f = open('C:\\Users\\rutski\\Desktop\\hello.txt', 'w')
f.write('Hello World!\n')
f.flush()
f.close()

and run with

> pythonw.exe source.py

This produces the file hello.txt with the expected output. Now I create a new Visual Studio solution. I make it Win32 project, choose "Windows Application" and "Empty Project." I create a new source file main.c and I copy-paste the source code of from pythonw (with Py_Main and all that). Now I add the following settings:

C:\Users\rutski\Documents\python\PCbuild\amd64 --- Library Search Directory
C:\Users\rutski\Documents\python\Include       --- Include Directory
C:\Users\rutski\Documents\python\PC            --- Include Directory (for pyconfig.h)

I choose "Debug | x64" and hit build. I pop open cmd.exe, browse to where mything.exe is, and execute

> mything.exe source.py

But this time nothing happens. No hello.txt gets created. I get no crash window or error message. I do not get thrown into a debugger. I just get no result. Am I missing some build flags here or something?

I'm running the exact same C code that pythonw.exe is, but mine isn't working. What gives?

I can't even seem to get Py_Main() to execute some python code from within my own application, so trying to write my own embed code is basically hopeless.

fieldtensor
  • 3,972
  • 4
  • 27
  • 43
  • Just posted a bug in the python issue tracker as well: https://mail.google.com/mail/u/0/#inbox/15dcb0731f605c44 – fieldtensor Aug 10 '17 at 07:26
  • In order to run your program in debug mode, so you can single step into it and get at least a general idea of what's happening, you can change the Project settings to include the command line you want to run. – Harry Johnston Aug 10 '17 at 09:35
  • 1
    Any `#include "Python.h"` is using the old crappy embedding API instead of the newer one: cffi. https://cffi.readthedocs.io/en/latest/embedding.html – habnabit Aug 10 '17 at 15:34

1 Answers1

0

What did you call your main function? "main"? WinMain? The program needs an entry point.

To see what is going on, run the program in a debugger and try to step through it. I suspect that it may not be getting to your "main" function.

If you are using main, try WinMain.

Or create the project not as an empty application but one with the skeleton of an application.

The way it looks like you are using it, the best choice may be to have the wizard create for you a console application.

If you are given a small app that compiles and runs and does nothing, but has the correct entry point, and you add your code there, you might have more success.

If you specifically want to use "main", and creating a console app is not a sufficient solution, then this link might help.

Basya
  • 1,477
  • 1
  • 12
  • 22
  • The question includes the code; the main function is wWinMain() which is perfectly valid AFAIK. Besides, if the build tools couldn't find the main function, wouldn't the build fail? – Harry Johnston Aug 10 '17 at 09:31
  • So when you say you copied the source code, you don't mean just the contents of the function but the entire file? – Basya Aug 10 '17 at 12:39
  • Have you tried stepping into your program in the debugger to see what it is doing? We can't figure that out from a description of the problem. The first sentence I wrote is the key to figuring out what is going on. When you know what is going on, you may have all the information you need. If, when you have seen what happens, you still don't understand why, or how to fix it, you can tell us what you found and we'll try to help you. – Basya Aug 10 '17 at 12:41
  • The OP didn't mention explicitly changing the character set configuration property, so *presumably* it is Unicode. But I've just checked (on Visual Studio 2010) and wWinMain() works perfectly well even if the multibyte character set is selected. – Harry Johnston Aug 10 '17 at 22:08