Anonymous
Получение ошибки 1407 при попытке создать окно на C#
Сообщение
Anonymous » 23 окт 2024, 04:30
Я начал изучать Windows API и пытаюсь создать окно на C#, но всегда получаю ошибку 1407: «Невозможно найти класс окна». Я написал [MarshalAs(UnmanagedType.LPWStr)] для lpClassName и lpWindowName, но это не помогло.
Но регистрация класса окна возвращает 49856.
Вот мой код:
Код: Выделить всё
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern ushort RegisterClassEx(ref WNDCLASSEX lpwcx);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr CreateWindowExW(
uint dwExStyle,
[MarshalAs(UnmanagedType.LPWStr)]
string lpClassName,
[MarshalAs(UnmanagedType.LPWStr)]
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetModuleHandle(string lpModuleName);
[StructLayout(LayoutKind.Sequential)]
public struct WNDCLASSEX
{
public uint cbSize;
public uint style;
public IntPtr lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}
private static IntPtr WindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == 0x0010)
{
Environment.Exit(0);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
static void Main()
{
const string CLASS_NAME = "SampleWindowClass";
IntPtr hInstance = GetModuleHandle(null);
WNDCLASSEX wc = new WNDCLASSEX
{
cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)),
style = 0,
lpfnWndProc = Marshal.GetFunctionPointerForDelegate(new WndProcDelegate(WindowProc)),
hInstance = hInstance,
hbrBackground = (IntPtr)1,
lpszClassName = CLASS_NAME, // Имя класса
hIconSm = IntPtr.Zero,
hIcon = IntPtr.Zero,
hCursor = IntPtr.Zero,
cbClsExtra = 0,
cbWndExtra = 0,
lpszMenuName = null
};
ushort classAtom = RegisterClassEx(ref wc);
if (classAtom == 0)
{
int errorCode = Marshal.GetLastWin32Error();
Console.WriteLine("Class registration error: " + errorCode);
return;
}
IntPtr hwnd = CreateWindowExW(
0,
CLASS_NAME,
"Learn to Program Windows",
0x00040000,
100,
100,
500,
500,
IntPtr.Zero,
IntPtr.Zero,
hInstance,
IntPtr.Zero
);
if (hwnd == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
Console.WriteLine("Window creation error: " + errorCode);
return;
}
ShowWindow(hwnd, 1);
}
private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
В чем может быть проблема?
Подробнее здесь:
https://stackoverflow.com/questions/791 ... in-c-sharp
1729647030
Anonymous
Я начал изучать Windows API и пытаюсь создать окно на C#, но всегда получаю ошибку 1407: «Невозможно найти класс окна». Я написал [MarshalAs(UnmanagedType.LPWStr)] для lpClassName и lpWindowName, но это не помогло. Но регистрация класса окна возвращает 49856. Вот мой код: [code][DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern ushort RegisterClassEx(ref WNDCLASSEX lpwcx); [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern IntPtr CreateWindowExW( uint dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr GetModuleHandle(string lpModuleName); [StructLayout(LayoutKind.Sequential)] public struct WNDCLASSEX { public uint cbSize; public uint style; public IntPtr lpfnWndProc; public int cbClsExtra; public int cbWndExtra; public IntPtr hInstance; public IntPtr hIcon; public IntPtr hCursor; public IntPtr hbrBackground; public string lpszMenuName; public string lpszClassName; public IntPtr hIconSm; } private static IntPtr WindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { if (msg == 0x0010) { Environment.Exit(0); } return DefWindowProc(hWnd, msg, wParam, lParam); } static void Main() { const string CLASS_NAME = "SampleWindowClass"; IntPtr hInstance = GetModuleHandle(null); WNDCLASSEX wc = new WNDCLASSEX { cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)), style = 0, lpfnWndProc = Marshal.GetFunctionPointerForDelegate(new WndProcDelegate(WindowProc)), hInstance = hInstance, hbrBackground = (IntPtr)1, lpszClassName = CLASS_NAME, // Имя класса hIconSm = IntPtr.Zero, hIcon = IntPtr.Zero, hCursor = IntPtr.Zero, cbClsExtra = 0, cbWndExtra = 0, lpszMenuName = null }; ushort classAtom = RegisterClassEx(ref wc); if (classAtom == 0) { int errorCode = Marshal.GetLastWin32Error(); Console.WriteLine("Class registration error: " + errorCode); return; } IntPtr hwnd = CreateWindowExW( 0, CLASS_NAME, "Learn to Program Windows", 0x00040000, 100, 100, 500, 500, IntPtr.Zero, IntPtr.Zero, hInstance, IntPtr.Zero ); if (hwnd == IntPtr.Zero) { int errorCode = Marshal.GetLastWin32Error(); Console.WriteLine("Window creation error: " + errorCode); return; } ShowWindow(hwnd, 1); } private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [/code] В чем может быть проблема? Подробнее здесь: [url]https://stackoverflow.com/questions/79115298/getting-error-1407-when-trying-to-create-a-window-in-c-sharp[/url]