DLL中的CreateWindowEx()创build一个奇数标题的窗口

DLL中的CreateWindowEx()创build一个奇数标题的窗口,第1张

概述DLL中的CreateWindowEx()创build一个奇数标题窗口

感谢您花费了大量的时间来回答这个问题。

我试图创build一个DLL,从DLL内打开一个窗口。 我用C#运行创build的DLL。 该DLL是在VSC中创build的,而C#代码是使用VSC#编译的。

该窗口是通过调用C#中的Initalize(const char* Title)或Initalize(string Title)来实现的。 不pipe我怎么做,创build的窗口是创build的,运行的,但它的标题不是传递的string。 我尝试使用const wchar_t* , LPCSTR , LPCWSTR , System.String , [MarshalAs(UnmanagedType.LPStr)] , [MarshalAs(UnmanagedType.LPWStr)] 。 我试着复制传递的string到一个dynamic分配的数组,分配新/删除和malloc /免费。

我认为这是一个指针错误,但得到我最多的是在我的C ++代码printf("passed string: %s",Title)打印正确的标题到控制台,但我的窗口如下所示:

Apache模块命令parsing器原型

在Unix中创build“系统调用”

OpenfileByID失败,ERROR_ACCESS_DENIED

如何计算给定date的星期数?

在linux中将filter连接到原始套接字 – C

我的C ++代码是:

// GameInterface.cpp : defines the exported functions for the DLL application. // #include "GameInterface.h" #include <windows.h> // OpenGL was origionally implimented into here,and removed to be asked on StackOverflow. LRESulT WINAPI DLLWindowProc(HWND hwnd,UINT msg,WParaM wParam,LParaM lParam); HINSTANCE hInstance = NulL; ATOM wclAtom = NulL; HWND hWnd = NulL; HDC hDC = NulL; HglrC hRC = NulL; bool running = false; #if _DEBUG #include <stdio.h> #endif BOol APIENTRY DllMain( HMODulE hModule,DWORD ul_reason_for_call,LPVOID lpReserved ) { #if _DEBUG printf("GameInterface.dll::DllMain()n"); #endif switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: hInstance = hModule; break; case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: Shutdown(); break; } return TRUE; } GAMEINTERFACE_API int Initalize(const char* Title) { if (hWnd != NulL) return 0; #if _DEBUG printf("GameInterface.dll::Initalize("%s")n",Title); #endif int length = strlen(Title); char* name = new char[length+1]; strcpy(name,Title); WNDCLASSEXA wcl; wcl.cbSize = sizeof(WNDCLASSEXA); wcl.style = CS_OWNDC; wcl.lpfnWndProc = DLLWindowProc; wcl.cbClsExtra = 0; wcl.cbWndExtra = 0; wcl.hInstance = hInstance; wcl.hIcon = LoadIcon(NulL,IDI_APPliCATION); wcl.hCursor = LoadCursor(NulL,IDC_ARROW); wcl.hbrBackground = (HBrush)color_APPWORKSPACE; wcl.lpszMenuname = NulL; wcl.lpszClassname = name; wcl.hIconSm = LoadIcon(NulL,IDI_APPliCATION); wclAtom = RegisterClassExA(&wcl); #if _DEBUG printf(" Registering Classn"); #endif if (!wclAtom) { #if _DEBUG printf(" Error: Could not Register Class.nExiting with error: %in",GetLastError() ); #endif return 1; } #if _DEBUG printf(" Creating Windown"); #endif hWnd = CreateWindowExA(0,(LPCSTR)wclAtom,(LPCSTR)name,WS_OVERLAPPEDWINDOW,CW_USEDEFAulT,512,NulL,hInstance,NulL); if (hWnd == NulL) { #if _DEBUG printf(" Error: Window Could not be created.nExiting with error: %in",GetLastError() ); #endif return 2; } #if _DEBUG printf(" displaying Windown"); #endif // to reduce size removed the code to initalize an OpenGL 3.1 context ShowWindow(hWnd,SW_SHOW); UpdateWindow(hWnd); running = true; delete [] name; #if _DEBUG printf("Returning from GameInterface.dll::Initalize(const char*) with errors: %in",GetLastError() ); #endif return 0; } GAMEINTERFACE_API voID Shutdown() { if (running = false) return; #if _DEBUG printf("GameInterface.dll::Shutdown()n"); #endif running = false; wglMakeCurrent(NulL,NulL); if (hRC != NulL) wglDeleteContext(hRC); if (hDC != NulL) ReleaseDC(hWnd,hDC); hRC = NulL; hDC = NulL; DestroyWindow(hWnd); UnregisterClassA( (LPCSTR)wclAtom,hInstance); wclAtom = NulL; hWnd = NulL; running = false; } GAMEINTERFACE_API int Update() { if ( (running == false) && (hWnd == NulL) ) return 1; MSG msg; if ( PeekMessage(&msg,PM_REMOVE) ) { TranslateMessage(&msg); dispatchMessage(&msg); } if (running == false) return 1; return 0; } GAMEINTERFACE_API voID DrawFrame() { // Contained some OpenGL code that has Now been removed. } LRESulT WINAPI DLLWindowProc(HWND hwnd,LParaM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); running = false; break; // handle other messages. default: // anything we dont handle. return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; // just in case } // GameInterface.h : Outlines the exported functions for the DLL application. // #pragma once #ifdef GAMEINTERFACE_EXPORTS #define GAMEINTERFACE_API __declspec(dllexport) #else #define GAMEINTERFACE_API __declspec(dllimport) #endif extern "C" { GAMEINTERFACE_API int Initalize(const char* Title); GAMEINTERFACE_API voID Shutdown(); GAMEINTERFACE_API int Update(); GAMEINTERFACE_API voID DrawFrame(); };

和C#代码:

// GameInterface.cs // using System; using System.Runtime.InteropServices; class GameInterface { const string GameInterfacefile = "GameInterface_d.dll"; [Dllimport(GameInterfacefile)] public extern static int Initalize(string Title); [Dllimport(GameInterfacefile)] public extern static voID Shutdown(); [Dllimport(GameInterfacefile)] public extern static int Update(); [Dllimport(GameInterfacefile)] public extern static voID DrawFrame(); }; // Program.cs // using System; using System.Collections.Generic; using System.linq; using System.Text; class Program { public static voID Main() { string Title = "OpenGL Window Title"; if (GameInterface.Initalize(Title) != 0) return; while ( GameInterface.Update() == 0 ) { // game logic. GameInterface.DrawFrame(); } GameInterface.Shutdown(); } }

我很难过,现在已经有一段时间了。

标识符未加载到c

全球包括在VS2013上的windows上的path

调用windows计算器c#

将程序链接到Eclipse中的C ++项目? linux的

尝试在共享内存中写入时发生总线错误

你在你的C ++版本中定义了UNICODE和_UNICODE吗? 你需要做的就是让C#像这样谈话。

在您的C ++项目的Visual Studio属性中,在常规下,将字符集设置为使用Unicode字符集 。 仔细检查/D "UNICODE"和/D "_UNICODE"出现在C / C ++ /命令行页面上。

(相反的方法是将您的出口声明为ANSI,但这是一个较差的解决方案,您应该支持Unicode。)

这可能是因为代码需要ANSI。

如果你这样做会发生什么:

[Dllimport(GameInterfacefile,CharSet=CharSet.Ansi)] public extern static int Initalize(string Title)

总结

以上是内存溢出为你收集整理的DLL中的CreateWindowEx()创build一个奇数标题的窗口全部内容,希望文章能够帮你解决DLL中的CreateWindowEx()创build一个奇数标题的窗口所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1276539.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存