CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2008
    Location
    Cairo , Egypt
    Posts
    24

    Get a String from an IntPtr from a WPARAM

    i am using SendMessage to send a message with a LPTSTR in WPARAM from a dll to my vb.net form and i get it by overriding WndProc but the problem is when using Marshal.PtrToStringAuto on the WPARAM it doesn't work, the strange thing is that if i monitor WM_SETTEXT of the form and use PtrToStringAuto to get the string from LPARAM it works, i guess mine doesn't work that's because the dll is loaded by other application.


    C++ DLL Proc
    Code:
    LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    
    	if( (lParam!=NULL) && (nCode == HC_ACTION))
    	{
    		CWPSTRUCT *CwpStruct = (CWPSTRUCT *) lParam;
    		switch(CwpStruct->message)
    		{
    			case WM_SETTEXT:
    				SendMessageTimeout(hWnd,WM_USER + 2,WPARAM(CwpStruct->lParam),LPARAM(CwpStruct->hwnd),SMTO_ABORTIFHUNG,100,0);
    				break;
    		}
    	}
    	return CallNextHookEx(hHook,nCode,wParam,lParam);
    }
    VB.NET Code
    Code:
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Try
                    Select Case m.Msg
                        Case MyMsgs.WM_SETTEXT
                        'i successfully get the pointer but can't get the text
                            Me.Text = System.Runtime.InteropServices.Marshal.PtrToStringAuto(m.WParam)
            Catch ex As Exception
                Debug.WriteLine("Error Messages : " & ex.Message )
            Finally
                MyBase.WndProc(m)
            End Try
    End Sub
    Last edited by komalo; May 31st, 2009 at 07:39 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Get a String from an IntPtr from a WPARAM

    We'll need much more information, like :
    How did you implement this API function ¿
    How is it called ¿
    What do you want to do precisely in your program ¿

    PS, please don't type in bold format, it's quite straineous on the eyes

  3. #3
    Join Date
    Nov 2008
    Location
    Cairo , Egypt
    Posts
    24

    Re: Get a String from an IntPtr from a WPARAM

    Quote Originally Posted by HanneSThEGreaT View Post
    We'll need much more information, like :
    How did you implement this API function ¿
    How is it called ¿
    What do you want to do precisely in your program ¿

    PS, please don't type in bold format, it's quite straineous on the eyes
    it is not an api , my program has a global hook proc , i moniter WM_SETTEXT messages and send the LPSTR to my program through SendMessage to my Form, the hook proc works fine because i moniter other messages, and i receive the pointer successfully the problem that when using that IntPtr (the pointer) in Marshal.PtrToStringAuto i don't get the string, i even tried to send a normal message with my string to the form but i couldn't get the string

    PS, sorry for the bold font i modified the post it now

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Get a String from an IntPtr from a WPARAM

    SendMessage is an API, so it is an API.

    How did you implement this ¿
    Show us some code please, otherwise we'll sit here guessing until eternity.
    If we don't know how you have sent the message through the SendMessage API, we cannot know the format of the sent message, as well what you want to do with it.

  5. #5
    Join Date
    Nov 2008
    Location
    Cairo , Egypt
    Posts
    24

    Re: Get a String from an IntPtr from a WPARAM

    Quote Originally Posted by HanneSThEGreaT View Post
    SendMessage is an API, so it is an API.

    How did you implement this ¿
    Show us some code please, otherwise we'll sit here guessing until eternity.
    If we don't know how you have sent the message through the SendMessage API, we cannot know the format of the sent message, as well what you want to do with it.
    I know that it is an API , i was talking in general i don't implement any thing , BUT I USE SendMessage IN THE C++ HOOK DLL NOT VB.NET

    C++ DLL Proc
    Code:
    LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    
    	if( (lParam!=NULL) && (nCode == HC_ACTION))
    	{
    		CWPSTRUCT *CwpStruct = (CWPSTRUCT *) lParam;
    		switch(CwpStruct->message)
    		{
    			case WM_SETTEXT:
    				SendMessageTimeout(hWnd,WM_USER + 2,WPARAM(CwpStruct->lParam),LPARAM(CwpStruct->hwnd),SMTO_ABORTIFHUNG,100,0);
    				break;
    		}
    	}
    	return CallNextHookEx(hHook,nCode,wParam,lParam);
    }
    VB.NET Code
    Code:
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Try
                    Select Case m.Msg
                        Case MyMsgs.WM_SETTEXT
                        'i successfully get the pointer but can't get the text
                            Me.Text = System.Runtime.InteropServices.Marshal.PtrToStringAuto(m.WParam)
            Catch ex As Exception
                Debug.WriteLine("Error Messages : " & ex.Message )
            Finally
                MyBase.WndProc(m)
            End Try
    End Sub
    Last edited by komalo; May 29th, 2009 at 08:10 AM.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Get a String from an IntPtr from a WPARAM

    Quote Originally Posted by komalo View Post
    I know that it is an API ,
    Right, now we all know it is an API. I'm not here to argue, I just want the members to understand the situation.
    Quote Originally Posted by komalo View Post
    i was talking in general i don't implement any thing , BUT I USE SendMessage IN THE C++ HOOK DLL NOT VB.NET
    Quote Originally Posted by komalo View Post

    C++ DLL Proc
    Code:
    LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    
    	if( (lParam!=NULL) && (nCode == HC_ACTION))
    	{
    		CWPSTRUCT *CwpStruct = (CWPSTRUCT *) lParam;
    		switch(CwpStruct->message)
    		{
    			case WM_SETTEXT:
    				SendMessageTimeout(hWnd,WM_USER + 2,WPARAM(CwpStruct->lParam),LPARAM(CwpStruct->hwnd),SMTO_ABORTIFHUNG,100,0);
    				break;
    		}
    	}
    	return CallNextHookEx(hHook,nCode,wParam,lParam);
    }
    If this was included in the first post, we wouldn't even hav had an argument. Now we all know what the situation is, now we can help, agree ¿
    I'll play with this at home tonight

  7. #7
    Join Date
    Nov 2008
    Location
    Cairo , Egypt
    Posts
    24

    Re: Get a String from an IntPtr from a WPARAM

    Quote Originally Posted by HanneSThEGreaT View Post
    Right, now we all know it is an API. I'm not here to argue, I just want the members to understand the situation.

    If this was included in the first post, we wouldn't even hav had an argument. Now we all know what the situation is, now we can help, agree ¿
    I'll play with this at home tonight
    yes i know, i was bad in explaining it, any way i add the code to the main post
    thanks in advance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured