簡體   English   中英

C ++ Python關閉了我的程序

[英]C++ Python closes my program

我正在用C ++編寫Direct3D和Python的小程序。 我創建了我的窗口,一切正常。 但如果我試着調用“Py_Initialize();” 我的節目關閉了。

(它以代碼1結束)問題是什么?

編輯:以下是我的代碼的一些部分。

MainIncludes.h

#include "Windows.h"
#include <d3d9.h>
#pragma comment (lib, "d3d9.lib")

#include <d3dx9.h>
#pragma comment (lib, "d3dx9.lib")

main_d3dwindow.cpp

int WINAPI WinMain(HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpCmdLine,
               int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
                      L"WindowClass",
                      L"Program",
                      WS_OVERLAPPEDWINDOW,
                      300, 300,
                      800, 600,
                      NULL,
                      NULL,
                      hInstance,
                      NULL);

ShowWindow(hWnd, nCmdShow);
mainWindow = hWnd;

initD3D(hWnd);
init_python();

MSG msg;

while(TRUE)
{
    while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    if(msg.message == WM_QUIT)
        break;

    render_frame();
}

cleanD3D();

return msg.wParam;
}

main_python.cpp

#include "Python.h"
void init_python() {
     Py_Initialize();
}

據我所知,Py_Initialize()只是初始化Python本身,而不是作為交互式解釋器。 此時,Python正在運行,但正在等待執行命令。 另外,你不應該忘記調用Py_Finalize()來釋放內存。

void pythonShell() {
   Py_Initialize();
   std::string pythonCommand = "execfile('python_script.py')"; // file or python command
   PyRun_SimpleString(pythonCommand.c_str());
   FILE* fp = stdin;
   char filename[] = "Embedded";
   PyRun_InteractiveLoop(fp, filename);
   Py_Finalize();
}

PyRun_InteractiveLoop()函數如果需要,可以從C ++代碼執行交互式shell。 我認為這里發生的事情是,你只是初始化Python,就是這樣,它沒有任何事可做,因為你沒有傳遞任何腳本/命令。

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM