This code worked for IE8 and earlier. For IE9 it seems that IPersistStreamInit::Save() returns E_NOTIMPL. Any help is greatly appreciated!
HRESULT CHtmlCtrl::GetDocumentHTML(CString& strHTML) const
{
HRESULT hr = E_NOINTERFACE;
CComPtr<IHTMLDocument2> spHTMLDocument;
GetHtmlDocument()->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&spHTMLDocument);
if (spHTMLDocument)
{
CComQIPtr<IPersistStreamInit> spPSI;
spPSI = spHTMLDocument;
if (spPSI)
{
CStreamOnCString stream;
hr = spPSI->Save(static_cast<IStream*>(&stream), FALSE);
if (hr == S_OK)
{
if (!stream.CopyData(strHTML))
hr = E_OUTOFMEMORY;
}
}
}
return hr;
}
Seems to be a bug in the MFC as described and confirmed here. Fixed in Visual Studio 2012 RTM.
Related
How would someone detect when a user enters tablet mode on a Windows 10 device with Delphi code?
Can someone show a code example for this?
I don't want to detect if the user has a tablet or not. I simply want to see whether they're in tablet mode or not. What would be the best way to do this?
You can use UIViewSettings.UserInteractionMode API. Please refer to #Raymond blog: "How can I detect whether my PC is in tablet mode?", there are UWP and desktop ways in C++ you can refer to.
More detailed information you can check this thread.
But you need find out how to do in Delphi. There are some related issues hope they are helpful for you:
delphi - call external WinAPI function
Can we call Native Windows API from Delphi?
I deleted the previous variant (based on [SO]: How can I detect when Windows 10 enters tablet mode in a Windows Forms application? (#CheeseLover's answer) (pointed out by #Remko's comment)) because it's a totally different scenario (doesn't have anything to do with Win running on desktop).
I spent some time on [MS.DevBlogs]: Raymond - How can I detect whether my PC is in tablet mode? (pointed out in #RitaHan-MSFT's answer (+1)), and clearly, that's the way to go.
I don't know how to "translate" the code into Delphi, as many years passed since I wrote significant amounts of code in it (but I'm sure it's possible), so I did the next best thing: wrote a C++ .dll (containing a modified / improved version of Raymond's code) that is called from Delphi.
Note: VStudio is required to build the .dll, I used 2015 Community Edition, which is free and can be downloaded from [VStudio]: Visual Studio 2015 and other Products (you need an MS account though).
dll.cpp:
#include <wrl/client.h>
#include <windows.ui.viewmanagement.h>
#include <UIViewSettingsInterop.h>
#include <wrl/wrappers/corewrappers.h>
namespace WRL = Microsoft::WRL;
namespace VM = ABI::Windows::UI::ViewManagement;
class Backend {
public:
static Backend &instance() {
static Backend m_instance;
return m_instance;
}
WRL::ComPtr<IUIViewSettingsInterop> interop() { return m_interop; }
private:
Backend() {
HRESULT res = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
m_comInit = (res == S_OK) || (res == S_FALSE);
if (m_comInit || (res == RPC_E_CHANGED_MODE)) {
res = Windows::Foundation::GetActivationFactory(WRL::Wrappers::HStringReference(
RuntimeClass_Windows_UI_ViewManagement_UIViewSettings).Get(), &m_interop);
}
}
Backend(const Backend &other) = delete;
Backend &operator =(const Backend &other) = delete;
~Backend() {
if (m_interop) { m_interop.Reset(); }
if (m_comInit) { CoUninitialize(); }
}
bool m_comInit = false;
WRL::ComPtr<IUIViewSettingsInterop> m_interop = nullptr;
};
/*!
Gets Tablet mode value.
\param hwnd Window handle to get the mode for
\returns:
1 - Tablet mode ON
0 - Tablet mode OFF
-X - Error
*/
extern "C" __declspec(dllexport) int GetTabletMode(HWND hwnd) {
WRL::ComPtr<IUIViewSettingsInterop> interop = Backend::instance().interop();
if (!interop) { return -3; }
WRL::ComPtr<VM::IUIViewSettings> viewSettings;
HRESULT res = interop->GetForWindow(hwnd != NULL ? hwnd : GetConsoleWindow(), IID_PPV_ARGS(&viewSettings));
if (!viewSettings) { return -2; }
VM::UserInteractionMode currentMode;
res = viewSettings->get_UserInteractionMode(¤tMode);
int ret = -1;
switch (currentMode) {
case VM::UserInteractionMode_Mouse: ret = 0; break;
case VM::UserInteractionMode_Touch: ret = 1; break;
default: ret = -1;
}
viewSettings.Reset();
return ret;
}
Below is the Delphi relevant code (only the unit, as the rest can easily be manufactured, and there's no point placing it all here).
Unit0.pas:
unit Unit0;
interface
uses
Forms, Dialogs, Controls, StdCtrls, Classes;
type
TForm0 = class(TForm)
CheckButton: TButton;
procedure CheckButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form0: TForm0;
function GetTabletMode(hwnd: THandle): Integer cdecl; external 'TabletUtils.dll';
implementation
{$R *.dfm}
procedure TForm0.CheckButtonClick(Sender: TObject);
var
TabletModeStr: String;
begin
case GetTabletMode(Self.Handle) of
0 : TabletModeStr := 'OFF';
1 : TabletModeStr := 'ON';
else TabletModeStr := 'ERROR';
end;
MessageDlg('Tablet Mode: ' + TabletModeStr, mtInformation, [mbOK], 0);
end;
end.
Output:
[cfati#CFATI-5510-0:e:\Work\Dev\StackOverflow\q056321591]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x86
[prompt]> dir /b
App0.cfg
App0.dof
App0.dpr
App0.exe
App0.res
dll.cpp
other
Unit0.dcu
Unit0.ddp
Unit0.dfm
Unit0.pas
[prompt]> cl /nologo /DDLL /DNDEBUG /DUSRDLL /D_WINDOWS /MT dll.cpp /link /NOLOGO /DLL /OUT:TabletUtils.dll ole32.lib runtimeobject.lib
dll.cpp
Creating library TabletUtils.lib and object TabletUtils.exp
[prompt]> dir /b
App0.cfg
App0.dof
App0.dpr
App0.exe
App0.res
dll.cpp
dll.obj
other
TabletUtils.dll
TabletUtils.exp
TabletUtils.lib
Unit0.dcu
Unit0.ddp
Unit0.dfm
Unit0.pas
[prompt]> App0.exe
[prompt]>
In the screenshot below, I ran the application:
On my laptop (Win 10) with Desktop mode (right side)
On a Win 10 VM with Tablet mode (left side). Note that I had to copy:
App0.exe
TabletUtils.dll
I am getting a strange error when creating the DirectX 12 command queue.
Other DX12 applications are able to launch successfully on the same machine.
My computer uses the D3D_FEATURE_LEVEL_11_0 if block.
The graphics card used for testing is NVIDIA GT 740, with 361.75 drivers
This is the code in use (minimized):
#include <Windows.h>
#include <d3d12.h>
#include <dxgi1_4.h>
#include <comdef.h>
#include <D3d12sdklayers.h>
#include <string>
#pragma comment(lib,"d3d12.lib")
#pragma comment(lib,"dxgi.lib")
#pragma comment(lib,"d3dcompiler.lib")
using namespace std;
LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(nCmdShow);
UNREFERENCED_PARAMETER(lpCmdLine);
wchar_t* WindowClass = L"Papergate";
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
wc.lpszClassName = WindowClass;
if (!RegisterClassEx(&wc))
{
return 1;
}
HWND hwnd = CreateWindowEx(NULL, wc.lpszClassName, WindowClass,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!hwnd)
{
UnregisterClass(WindowClass, hInstance);
return 1;
}
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
ID3D12Device* device;
HRESULT result = D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_12_1,
__uuidof(ID3D12Device), (void**)&device);
if (FAILED(result))
{
result = D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_12_0,
__uuidof(ID3D12Device), (void**)&device);
if (FAILED(result))
{
result = D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_11_0,
__uuidof(ID3D12Device), (void**)&device);
if (FAILED(result)) {
_com_error error(result);
MessageBox(hwnd, error.ErrorMessage(),
(wstring(L"Error: ") + to_wstring(__LINE__)).c_str(),
MB_OK);
return 2;
}
}
}
ID3D12Debug* debugInterface;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugInterface))))
{
debugInterface->EnableDebugLayer();
}
D3D12_COMMAND_QUEUE_DESC commandQueueDesc;
commandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
commandQueueDesc.NodeMask = 0;
commandQueueDesc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
commandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
ID3D12CommandQueue* commandQueue;
result = device->CreateCommandQueue(&commandQueueDesc, __uuidof(ID3D12CommandQueue), (void**)&commandQueue);
if (FAILED(result)) {
_com_error error(result);
MessageBox(hwnd, error.ErrorMessage(),
(wstring(L"Error: ") + to_wstring(__LINE__)).c_str(), MB_OK);
result = device->GetDeviceRemovedReason();
error = _com_error(result);
MessageBox(hwnd, error.ErrorMessage(),
(wstring(L"Error: ") + to_wstring(__LINE__)).c_str(), MB_OK);
debugInterface->Release(); device->Release(); return 2;
}
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (GetMessage(&msg, NULL, 0, 0) && msg.message != WM_QUIT)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
commandQueue->Release();
device->Release();
UnregisterClass(WindowClass, hInstance);
return 0;
}
I am getting the following errors on lines 97 and 102, respectively:
The GPU device instance has been suspended. Use GetDeviceRemovedReason to determine the appropriate action.
Second error:
The GPU will not respond to more commands, most likely because some other application submitted invalid commands.
The calling application should re-create the device and continue.
This seems quite likely to be a driver bug of some kind. Check to see if there are updated drivers for your hardware. You should try using the Direct3D12 Game templates in this VSIX and see if they hit the same kind of issue (for more details on the templates see this blog post).
Your cascade pattern of calling D3D12CreateDevice for various feature levels is unusual and is not necessary. If your application can run on Direct3D Feature Level 11.0 or greater, then just use D3D_FEATURE_LEVEL_11_0 once. You should pass whatever your minimum supported feature level is to this function.
If the Direct3D 12 device does support a higher feature level, you can discover that by using CheckFeatureSupport either by checking for the individual features or by using D3D12_FEATURE_FEATURE_LEVELS:
// Create the DX12 API device object.
DX::ThrowIfFailed(D3D12CreateDevice(
adapter.Get(),
m_d3dMinFeatureLevel,
IID_PPV_ARGS(&m_d3dDevice)
));
// Determine maximum supported feature level for this device
static const D3D_FEATURE_LEVEL s_featureLevels[] =
{
D3D_FEATURE_LEVEL_12_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
};
D3D12_FEATURE_DATA_FEATURE_LEVELS featLevels =
{
_countof(s_featureLevels), s_featureLevels, D3D_FEATURE_LEVEL_11_0
};
HRESULT hr = m_d3dDevice->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS,
&featLevels, sizeof(featLevels));
if (SUCCEEDED(hr))
{
m_d3dFeatureLevel = featLevels.MaxSupportedFeatureLevel;
}
else
{
m_d3dFeatureLevel = m_d3dMinFeatureLevel;
}
Keep in mind that D3D_FEATURE_LEVEL_12_0 and D3D_FEATURE_LEVEL_12_1 are essentially just D3D_FEATURE_LEVEL_11_1 with a few optional features made mandatory. If your app is already checking for them at 11.x then there's no reason to 'require' 12.0 or 12.1. See MSDN.
For the vast majority of Direct3D 12 games & applications, D3D_FEATURE_LEVEL_11_0 or D3D_FEATURE_LEVEL_11_1 are good choices. Keep in mind that while AMD/ATI supported Feature Level 11.1 pretty early, NVIDIA DirectX 11 parts only supported 11.0 with some optional features for some time.
My problem with this code is that when I run it in visual C++ A window pops up
but then it just crashes. It is not responding and I cannot click exit. I have to pull up the
task manager to get rid of the window. I am new to windows programming and direct X.
Below I will post were I think the problem is.
#include <d3d9.h>
#include <time.h>
#define APPTITLE "Direct3D_Windowed"
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE);
int Game_Init(HWND);
void GAME_RUN(HWND);
void GAME_END(HWND);
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
// Over here, after GAME_END() is called, I tried separating the POSTQUITMESSAGE But I
I just got an error.
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg )
{
case WM_DESTROY:
GAME_END(hWnd);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;
return RegisterClassEx(&wc);
}
// I got this code from a book that I am reading and realized that WinProc is not being
called in this function. Is this the potential problem? Were would I put the WinProc
function call if it is supposed to be here in WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg = {0};
MyRegisterClass(hInstance);
HWND hWnd;
hWnd = CreateWindow(
APPTITLE,
APPTITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
400,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if(!Game_Init(hWnd))
return 0;
int done = 0;
while(!done)
{
if(msg.message == WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
MessageBox(hWnd, "Recieve WM_QUIT message", "WinMain", MB_OK);
done = 1;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
GAME_RUN(hWnd);
}
return msg.wParam;
}
int Game_Init(HWND hWnd)
{
MessageBox(hWnd, "Program is about to start", "Game_Init", MB_OK);
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d == NULL)
{
MessageBox(hWnd, "Error initializing Direct3D", "Error", MB_OK);
return 0;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
if(d3ddev == NULL)
{
MessageBox(hWnd, "Error creating Direct device", "Error", MB_OK);
return 0;
}
srand(time(NULL));
return 1;
}
void GAME_RUN(HWND hWnd)
{
if(d3ddev == NULL)
return;
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 0);
if(d3ddev->BeginScene())
{
d3ddev->EndScene();
}
d3ddev->Present(NULL, NULL, NULL, NULL);
}
void GAME_END(HWND hWnd)
{
MessageBox(hWnd, "Program is about to end", "Game End", MB_OK);
if(d3ddev != NULL)
d3ddev->Release();
if(d3d != NULL)
d3d->Release();
}
Have a look at this?
if(msg.message == WM_QUIT)
In your while-loop.
Perhaps change that to, say:
if(true)
Reason: you want your application to pass on all messages, not just the ones that cause it to quit. Say for instance when windows wants your application to draw itself. Basically, your current code doenst allow your application to do anything except quitting.
If you want to do something special when the application quits, add another case WM_QUIT: after the already existing case WM_DESTROY: in WinProc().
The current location for GAME_RUN(hWnd); will not work out for you.
You want to either put that in a seperate thread (easiest, and highest performance). Or you want to use some timers, and handle it with case WM_TIMER: after your case WM_DESTROY:. Or alternatively make up your own user defined message.
Code as below. How to avoid the TYPE_E_LIBNOTREGISTERED(hr=0x8002801D, Library not registered) error(DO NOT register the ActiveX to Windows) of disp->Invoke()?
When register the ActiveX control, all is OK, but I want not register to WINDOWS.
Thanks!
#define WIN32_LEAN_AND_MEAN
#include <tchar.h>
#include <Windows.h>
#include <assert.h>
#include <stdio.h>
#include <ole2.h>
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
OleInitialize(NULL);
LPCTSTR lpFile = _T("D:\\Temp\\Flash32_11_4_402_287.ocx");
HMODULE hDll = LoadLibrary(lpFile);
if (hDll)
{
typedef HRESULT (WINAPI *DllGetClassObject_t)(REFCLSID, REFIID, LPVOID*);
DllGetClassObject_t DllGetClassObject = NULL;
DllGetClassObject = (DllGetClassObject_t)GetProcAddress(hDll, "DllGetClassObject");
if (DllGetClassObject)
{
CLSID clsid = GUID_NULL;
IClassFactory *pCF = NULL;
if (SUCCEEDED(CLSIDFromString(L"{D27CDB6E-AE6D-11CF-96B8-444553540000}", &clsid))
&& SUCCEEDED(DllGetClassObject(clsid, IID_IClassFactory, (void**)&pCF)))
{
IOleObject *obj = NULL;
IDispatch *disp = NULL;
if (SUCCEEDED(pCF->CreateInstance(NULL, IID_IOleObject, (void**)&obj))
&& SUCCEEDED(obj->QueryInterface(IID_IDispatch, (void**)&disp)))
{
DISPPARAMS params = { NULL, NULL, 0, 0 };
HRESULT hr = disp->Invoke(0x70, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
¶ms, NULL, NULL, NULL);
assert(SUCCEEDED(hr));
//ERROR: hr=0x8002801D,TYPE_E_LIBNOTREGISTERED, Library not registered
}
if (disp) disp->Release();
if (obj) obj->Release();
pCF->Release();
}
}
}
OleUninitialize();
return 0;
}
To Hans Passant:
Thanks very very much. English is not my mother tongue, but under you detailed answers, I have the perfect solution to this problem, and this is my first question on stackoverflow, thanks again. As you said, my solution is bellow:
//ITypeInfo *m_ti = NULL;
//pCF->CreateInstance(...);
ITypeLib *tl = NULL;
if (SUCCEEDED(LoadTypeLib(wsOcxFile, &tl)))
{
if (SUCCEEDED(tl->GetTypeInfoOfGuid(guid, &m_ti)))
{
hr = m_ti->Invoke(disp,
dispid, wFlags, &m_dispParams,
pvResult, &ei, &nArgErr);
}
tl->Release();
}
//m_ti->Release();
Microsoft DxDiag can detect whether a system has "Direct3D Acceleration".
If the system has not the capability, DxDiag will write "Direct3D Acceleration not available" and will write in the console "Direct3D functionality not available. You should verify that the driver is a final version from the hardware manufacturer."
I would like the same with a C++ function.
I made some tests and the following function seems to do the job.
Any other better idea?
Thank you.
Alessandro
#include <ddraw.h>
#include <atlbase.h>
bool has3D()
{
CComPtr< IDirectDraw > dd;
HRESULT hr = ::DirectDrawCreate( 0, &dd, 0 );
if ( hr != DD_OK ) return false;
DDCAPS hel_caps, hw_caps;
::ZeroMemory( &hel_caps, sizeof( DDCAPS ) );
::ZeroMemory( &hw_caps, sizeof( DDCAPS ) );
hw_caps.dwSize = sizeof( DDCAPS );
hel_caps.dwSize = sizeof( DDCAPS );
hr = dd->GetCaps( &hw_caps, &hel_caps );
if ( hr != DD_OK ) return false;
return (hw_caps.dwCaps & DDCAPS_3D) && (hel_caps.dwCaps & DDCAPS_3D);
}
As DirectDraw is now deprecated, it's maybe preferable to use the Direct3D functions.
If the purpose is to detect if 3D acceleration is available for an application, I would initialize Direct3D and then check if the HAL Device Type is available.
LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION );
D3DCAPS9 caps;
if ( FAILED(d3d->GetDeviceCaps(D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL, &caps)) )
{
return false;
}
You can check the validity of this code by forcing the software rendering in the DirectX Control Panel by checking the "Software only" checkbox in the Direct3D tab.
Test the code with and without the checkbox checked and see if it suits your needs.
You can access DX Diag via IDXDiagContainer and IDXDiagProvider