Delphi-PRAXiS
Seite 3 von 3     123   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Convert sample from platform SDK to Delphi (https://www.delphipraxis.net/82882-convert-sample-platform-sdk-delphi.html)

ste_ett 22. Dez 2006 11:26

Re: Convert sample from platform SDK to Delphi
 
"this" is a pointer to the current instance of the class.

"(LPVOID)" turns the pointer from a pointer to a class into a normal pointer. :)

Remko 22. Dez 2006 11:59

Re: Convert sample from platform SDK to Delphi
 
Not sure I understand what you mean. Can you show me the Delphi translation?

ste_ett 22. Dez 2006 12:07

Re: Convert sample from platform SDK to Delphi
 
Delphi-Quellcode:

m_hwndHidden := CreateWindowEx(  ...,
                            ...,


                                Self);

Remko 22. Dez 2006 12:10

Re: Convert sample from platform SDK to Delphi
 
Thanks Stefan, you confirm what I made of it. On to the next one :-)

Robert Marquardt 22. Dez 2006 12:14

Re: Convert sample from platform SDK to Delphi
 
The conversion of "(LPVOID) this" is "Pointer(Self)". _HiddenWindowProc receives a WM_CREATE message where the LParam points to a CREATESTRUCT structure. The lpCreateParams element of that structure is the value handed to CreateWindowEx. This allows to jump back into object land inside of _HiddenWindowProc function by casting the pointer back and then having access to the object again.

Remko 22. Dez 2006 12:24

Re: Convert sample from platform SDK to Delphi
 
Thanks Robert, that makes it clear. (I understand now that I didn't take the most easy example to start with for a first conversion, but I'm learning not also the conversion itself but a lot more! But hey, I guess it's Luctor et Emergo!)

Remko 22. Dez 2006 13:00

Re: Convert sample from platform SDK to Delphi
 
Liste der Anhänge anzeigen (Anzahl: 1)
Just posting some progress (attached). I'm currently stuck at _HiddenWindowProc()
Code:
   CPropSheetHost *pThis = (CPropSheetHost*)((LONG_PTR)GetWindowLongPtr(hWnd, VIEW_POINTER_OFFSET));

    switch (uMessage)
    {
    case WM_NCCREATE:
        {
            LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
            pThis = (CPropSheetHost*)(lpcs->lpCreateParams);
            ::SetWindowLongPtr(hWnd, VIEW_POINTER_OFFSET, (LONG)(LONG_PTR)pThis);
I'm going to stop for the moment, I was obviously too optimistic I'd be able to finish it today. Fröhliche Weihnachten und ein glückliches Neues Jahr to everyone here.

Robert Marquardt 22. Dez 2006 13:17

Re: Convert sample from platform SDK to Delphi
 
Code:
   CPropSheetHost *pThis = (CPropSheetHost*)((LONG_PTR)GetWindowLongPtr(hWnd, VIEW_POINTER_OFFSET));
This is a local variable which is immediately initialized. GetWindowLongPtr supersedes GetWindowLong. You can use GetWindowLong instead.
The value pulled is the this or Self value handed in as parameter to CreateWindowEx.
Code:
    switch (uMessage)
    {
    case WM_NCCREATE:
        {
            LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
            pThis = (CPropSheetHost*)(lpcs->lpCreateParams);
            ::SetWindowLongPtr(hWnd, VIEW_POINTER_OFFSET, (LONG)(LONG_PTR)pThis);
Now here the value is extracted from the CREATESTRUCT structure and stuffed into the extra data area of the window.
This works because WM_NCCREATE is about the first message received so the above variable initialization almost always pulls the value.
It just fails for WM_NCCREATE itself where it gets an uninitialized value, but in this case the variable is initialized from the CREATESTRUCT.
It is a bit overcoded and it contains a bug. The code is not 64 bit safe. (LONG)(LONG_PTR) is a double typecast which first casts the pointer to the type LONG_PTR. LONG_PTR can hold a 64 bit value. Unfortunately it is then casted to LONG which can hold only 32 bits. So the second typecast is actually a bug.

So this is just a trick to be always able to return to object land each time _HiddenWindowProc is called with a message.
Delphi-Quellcode:
var
  Form: TForm1; // or whatever type you have handed in to CreateWindowEx
begin
  Form := GetWindowLong(hWnd, VIEW_POINTER_OFFSET));
  case Msg of
    WM_NCCREATE:
      SetWindowLong(hWnd, VIEW_POINTER_OFFSET, LONG(LPCREATESTRUCT(lParam).lpCreateParams));
This is the trick in Delphi. You can of course drop all this and just access the global variable Form1 Delphi has created for you already.

Remko 27. Dez 2006 10:17

Re: Convert sample from platform SDK to Delphi
 
OK, so:
Code:
LRESULT CALLBACK CPropSheetHost::_HiddenWindowProc( HWND hWnd,
                                                    UINT uMessage,
                                                    WPARAM wParam,
                                                    LPARAM lParam)
{
    CPropSheetHost *pThis = (CPropSheetHost*)((LONG_PTR)GetWindowLongPtr(hWnd, VIEW_POINTER_OFFSET));

    switch (uMessage)
    {
    case WM_NCCREATE:
        {
            LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
            pThis = (CPropSheetHost*)(lpcs->lpCreateParams);
            ::SetWindowLongPtr(hWnd, VIEW_POINTER_OFFSET, (LONG)(LONG_PTR)pThis);
        }
        break;

    case WM_CREATE:
        break;

    case WM_DESTROY:
        break;

    case WM_ADSPROP_NOTIFY_CHANGE:
        OutputDebugString(TEXT("WM_ADSPROP_NOTIFY_CHANGE\n"));
        break;

    case WM_DSA_SHEET_CREATE_NOTIFY:
        {
            PDSA_SEC_PAGE_INFO pSecPageInfo;
           
            // Extract the secondary sheet information from the wParam.
            if(S_OK == pThis->_ExtractSecPageInfo(wParam, &pSecPageInfo))
            {
                // Create a secondary property sheet.
                pThis->_CreateSecondaryPropertySheet(pSecPageInfo);
            }
            else
            {
                // Even if the extraction failed, the wParam needs to be freed.
                pSecPageInfo = (PDSA_SEC_PAGE_INFO)wParam;
            }

            /*
            The receiver of the message must free the DSA_SEC_PAGE_INFO
            structure when it is no longer needed.
            */
            LocalFree(pSecPageInfo);
        }
        return 0;

    case WM_DSA_SHEET_CLOSE_NOTIFY:
        if(PROP_SHEET_HOST_ID == wParam)
        {
            OutputDebugString(TEXT("PROP_SHEET_HOST_ID\n"));
        }
        return 0;

    default:
        break;
    }

    return DefWindowProc(hWnd, uMessage, wParam, lParam);
}
Delphi-Quellcode:
function TPropSheetHost._HiddenWindowProc(hWnd: HWnd; Msg: UINT; WParam: WPARAM; LParam: LPARAM): UINT; stdcall;
var lpcs: TCreateStruct;
  PropSheetHost: TPropSheetHost;
  pSecPageInfo: PDSA_SEC_PAGE_INFO;
  begin
  PropSheetHost := TPropSheetHost(GetWindowLong(hWnd, VIEW_POINTER_OFFSET));
  case Msg of
    WM_NCCREATE: SetWindowLong(hWnd, VIEW_POINTER_OFFSET, LONG(LPCREATESTRUCT(lParam).lpCreateParams));
    WM_CREATE:;
    WM_DESTROY:;
    WM_ADSPROP_NOTIFY_CHANGE: OutputDebugString('WM_ADSPROP_NOTIFY_CHANGE'#10#13);
    WM_DSA_SHEET_CREATE_NOTIFY:
    begin
      // Extract the secondary sheet information from the wParam.
      if _ExtractSecPageInfo(wParam, pSecPageInfo) = S_OK then
      begin
        // Create a secondary property sheet.
        _CreateSecondaryPropertySheet(pSecPageInfo);
      end
      else begin
        // Even if the extraction failed, the wParam needs to be freed.
        pSecPageInfo := PDSA_SEC_PAGE_INFO(wParam);
      end;
      {*
      The receiver of the message must free the DSA_SEC_PAGE_INFO
      structure when it is no longer needed.
      *}
      LocalFree(Cardinal(pSecPageInfo));
      Result := 0;
      Exit;
    end;
    WM_DSA_SHEET_CLOSE_NOTIFY:
    begin
      if PROP_SHEET_HOST_ID = wParam then
      begin
        OutputDebugString('PROP_SHEET_HOST_ID'#10#13);
      end;
      Result := 0;
    end;
  end;
  Result := DefWindowProc(hWnd, Msg, wParam, lParam);
end;

Remko 27. Dez 2006 11:02

Re: Convert sample from platform SDK to Delphi
 
Liste der Anhänge anzeigen (Anzahl: 1)
Is this translation correct?

Code:
HRESULT CPropSheetHost::_AddPagesForObject(IADs *padsObject)
{
    HRESULT hr;
   
    // Get a copy of our IDataObject.
    CComPtr<IDataObject> spDataObject;
    hr = this->QueryInterface(IID_IDataObject, (LPVOID*)&spDataObject);
    if(FAILED(hr))
    {
        return hr;
    }
    < CUT >
Delphi-Quellcode:
function TPropSheetHost._AddPagesForObject(hPage: HPROPSHEETPAGE; lParam: LPARAM): HRESULT;
var hr: HResult;
 spDataObject: IDataObject;

begin
  // Get a copy of our IDataObject.
  spDataObject := CreateComObject(IID_IDataObject) as IDataObject;
  hr := QueryInterface(IID_IDataObject, spDataObject);
  if Failed(hr) then
  begin
    Result := hr;
    Exit;
  end;
    < CUT >
Some other questions:
How to cast a WideString to PAnsiChar? //Edit: PAnsiChar := PChar(String(WideString));

Code:
    // Fill out the PROPSHEETHEADER structure.
    psh.dwSize          = sizeof(PROPSHEETHEADER);
    psh.dwFlags         = PSH_DEFAULT;
    psh.hwndParent      = m_hwndParent;
    psh.hInstance       = NULL;
    psh.pszIcon         = NULL;
    psh.pszCaption      = W2T(sbstrTemp);
    psh.nPages          = (UINT)m_rgPageHandles.GetSize();
    psh.phpage          = m_rgPageHandles.GetData();
    psh.pfnCallback     = NULL;
GetSize returns number of elements in array? --> psh.nPages := Length(m_rgPageHandles.GetSize)?
Getdata stores all members in a pointer? How to do this in Delphi? psh.u3.phpage := @m_rgPageHandles;?

Edit: adding one more question, how to convert this:
Code:
            LPBYTE pDest = (LPBYTE)*ppSecPageInfo;
            LPBYTE pSource = (LPBYTE)wParam;

            // Copy the original memory to the new block.
            CopyMemory(pDest + dwOffset, pSource, sizeOldSize);
This part CopyMemory(pDest + dwOffset will not work in Delphi. // EDIT: CopyMemory(PChar(pDest) + dwOffset, pSource, sizeOldSize); ?

Added code so far as attachment


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:29 Uhr.
Seite 3 von 3     123   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz