<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[WM_DEVICECHANGE: Zumachen, wenn usb-stick entfernt wird]]></title><description><![CDATA[<p>Hallo Leute.</p>
<p>Wie der Titel sagt, möchte ich meine exe, die von autorun.inf von usb-stick gestartet wird, schließen, wenn der stick entfernt werden soll.</p>
<pre><code class="language-cpp">LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static HANDLE hModuleFile;

	PDEV_BROADCAST_HDR deviceHDR;
	PDEV_BROADCAST_HANDLE deviceHandle;

	switch (uMsg)
	{
	case WM_CREATE:
		// diese funktion öffnet ein Handle von CreateFile, dass von GetModuleFileName(0) kommt
		hModuleFile = GetModuleHandle(0);

		deviceHandle-&gt;dbch_handle = hModuleFile;
		deviceHandle-&gt;dbch_devicetype = DBT_DEVTYP_HANDLE;
		RegisterDeviceNotification(hWnd, deviceHandle, DEVICE_NOTIFY_WINDOW_HANDLE);

		return 0;
	case WM_DESTROY:
		CloseHandle(hModuleFile);
		PostQuitMessage(0);
		return 0;
	case WM_DEVICECHANGE:
		switch (wParam)
		{
		case DBT_DEVICEQUERYREMOVE:
			deviceHDR = (PDEV_BROADCAST_HDR)lParam;
			if (deviceHDR-&gt;dbch_devicetype == DBT_DEVTYP_HANDLE)
				// die exe soll geschlossen werden
				// also zum test piepen
				Beep(440, 1000);
				// aber bei meinen Tests piepte er net...
			return 0;
		}
		return 0;
	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}
}

HANDLE OpenModuleHandle(HMODULE hModule)
{
	WCHAR fileName[MAX_PATH + 1];
	GetModuleFileName(hModule, fileName, MAX_PATH);

	return CreateFile(fileName,
					  GENERIC_READ,
					  FILE_SHARE_READ,
					  NULL,
					  OPEN_EXISTING,
					  FILE_ATTRIBUTE_NORMAL,
					  NULL);
}
</code></pre>
<p>Was könnte daran falsch sein?</p>
<p>Den Code habe ich von MSDN:<br />
<a href="http://msdn2.microsoft.com/en-us/library/aa363427(VS.85).aspx" rel="nofollow">http://msdn2.microsoft.com/en-us/library/aa363427(VS.85).aspx</a></p>
<p>Vielen Dank für eure Hilfe!!! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/205059/wm_devicechange-zumachen-wenn-usb-stick-entfernt-wird</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 15:33:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/205059.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 09 Feb 2008 22:45:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to WM_DEVICECHANGE: Zumachen, wenn usb-stick entfernt wird on Sat, 09 Feb 2008 22:45:35 GMT]]></title><description><![CDATA[<p>Hallo Leute.</p>
<p>Wie der Titel sagt, möchte ich meine exe, die von autorun.inf von usb-stick gestartet wird, schließen, wenn der stick entfernt werden soll.</p>
<pre><code class="language-cpp">LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static HANDLE hModuleFile;

	PDEV_BROADCAST_HDR deviceHDR;
	PDEV_BROADCAST_HANDLE deviceHandle;

	switch (uMsg)
	{
	case WM_CREATE:
		// diese funktion öffnet ein Handle von CreateFile, dass von GetModuleFileName(0) kommt
		hModuleFile = GetModuleHandle(0);

		deviceHandle-&gt;dbch_handle = hModuleFile;
		deviceHandle-&gt;dbch_devicetype = DBT_DEVTYP_HANDLE;
		RegisterDeviceNotification(hWnd, deviceHandle, DEVICE_NOTIFY_WINDOW_HANDLE);

		return 0;
	case WM_DESTROY:
		CloseHandle(hModuleFile);
		PostQuitMessage(0);
		return 0;
	case WM_DEVICECHANGE:
		switch (wParam)
		{
		case DBT_DEVICEQUERYREMOVE:
			deviceHDR = (PDEV_BROADCAST_HDR)lParam;
			if (deviceHDR-&gt;dbch_devicetype == DBT_DEVTYP_HANDLE)
				// die exe soll geschlossen werden
				// also zum test piepen
				Beep(440, 1000);
				// aber bei meinen Tests piepte er net...
			return 0;
		}
		return 0;
	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}
}

HANDLE OpenModuleHandle(HMODULE hModule)
{
	WCHAR fileName[MAX_PATH + 1];
	GetModuleFileName(hModule, fileName, MAX_PATH);

	return CreateFile(fileName,
					  GENERIC_READ,
					  FILE_SHARE_READ,
					  NULL,
					  OPEN_EXISTING,
					  FILE_ATTRIBUTE_NORMAL,
					  NULL);
}
</code></pre>
<p>Was könnte daran falsch sein?</p>
<p>Den Code habe ich von MSDN:<br />
<a href="http://msdn2.microsoft.com/en-us/library/aa363427(VS.85).aspx" rel="nofollow">http://msdn2.microsoft.com/en-us/library/aa363427(VS.85).aspx</a></p>
<p>Vielen Dank für eure Hilfe!!! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452821</guid><dc:creator><![CDATA[Script-Styler]]></dc:creator><pubDate>Sat, 09 Feb 2008 22:45:35 GMT</pubDate></item><item><title><![CDATA[Reply to WM_DEVICECHANGE: Zumachen, wenn usb-stick entfernt wird on Sat, 09 Feb 2008 23:01:11 GMT]]></title><description><![CDATA[<p>Ich weiß jetzt nicht direkt, was daran nicht funktioniert, aber kann es sein, dass in <code>PDEV_BROADCAST_HANDLE deviceHandle;</code> ein Fehler liegt? Das müsste doch ein Zeiger auf die DEV_BROADCAST_HANDLE-Struktur sein, der Zeiger ist aber uninitialisiert. Evtl direkt eine Instanz von DEV_BROADCAST_HANDLE erstellen und bei RegisterDeviceNotification einen Zeiger darauf übergeben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452827</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Sat, 09 Feb 2008 23:01:11 GMT</pubDate></item><item><title><![CDATA[Reply to WM_DEVICECHANGE: Zumachen, wenn usb-stick entfernt wird on Sat, 09 Feb 2008 23:28:50 GMT]]></title><description><![CDATA[<p>bei der msdn stand das auch so. (siehe link)</p>
<p>welche fehler seht ihr noch?</p>
<p>Könnt ihr den verbesserten code posten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452834</guid><dc:creator><![CDATA[Script-Styler]]></dc:creator><pubDate>Sat, 09 Feb 2008 23:28:50 GMT</pubDate></item></channel></rss>