Click here to Skip to main content
15,912,578 members
Home / Discussions / C#
   

C#

 
GeneralIQueryCancelAutoPlay Pin
myker7-Dec-04 18:26
myker7-Dec-04 18:26 
GeneralRe: IQueryCancelAutoPlay Pin
Daniel Turini7-Dec-04 20:23
Daniel Turini7-Dec-04 20:23 
GeneralRe: IQueryCancelAutoPlay Pin
Heath Stewart8-Dec-04 7:40
protectorHeath Stewart8-Dec-04 7:40 
GeneralRe: IQueryCancelAutoPlay Pin
Daniel Turini8-Dec-04 8:00
Daniel Turini8-Dec-04 8:00 
GeneralRe: IQueryCancelAutoPlay Pin
Heath Stewart8-Dec-04 8:07
protectorHeath Stewart8-Dec-04 8:07 
Questionhow make a mirror printing in c# Pin
bigmega7-Dec-04 17:27
bigmega7-Dec-04 17:27 
AnswerRe: how make a mirror printing in c# Pin
Christian Graus7-Dec-04 18:33
protectorChristian Graus7-Dec-04 18:33 
GeneralProblem with SendMessage() using P/Invoke Pin
Liu Shuai7-Dec-04 17:15
Liu Shuai7-Dec-04 17:15 
Hi, I am having problem using Win32 SendMessage function to send a string from one process to another in a C# Windows Form app. I am using .Net 1.1 SP1 on a Windows XP Pro box.

What I am trying to do is to restrict user from running multiple instances of a windows form app. When they do "MyWindowsFormApp.exe userArgument", I want to pass "userArgument" to current running instance of the app.

Here is the code

/***dll import part***/
[DllImport("User32.dll", EntryPoint="SendMessage")]
public static extern int SendMessage( int hwnd, int uMsg, int wParam, [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lParam);

/***code that sends message***/
//this returns currently running process of my application.
Process process = GetCurrentInstance();
//get first argument
string s = GetArgument();
//send argument Win32.WM_USER = 0x0400;
SendMessage(process.MainWindowHandle.ToInt32(),Win32.WM_USER, 0, s);

/***code in main form of MyWindowsFormApp that receives message***/
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case Win32.WM_USER:
IntPtr intPtr = m.LParam;
string str = Marshal.PtrToStringAuto(intPtr);
string str2 = (string)m.GetLParam( typeof(string) );
break;
}
}

Sending message part seems working(not exception thrown) and the WndProc method in the windows form IS receiving the message.

Problems are:
intPtr.ToInt32() always returns 1242424. Marshal.PtrToStringAuto(intPtr) always returns a empty string.
and (string)m.GetLParam( typeof(string) ) throws a MissingMethodExcpetion ("No parameterless constructor defined for this object.")

What am I doing wrong? Any suggestion will be greatly appreciated!

LS


GeneralRe: Problem with SendMessage() using P/Invoke Pin
Daniel Turini7-Dec-04 20:02
Daniel Turini7-Dec-04 20:02 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Liu Shuai8-Dec-04 3:35
Liu Shuai8-Dec-04 3:35 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Daniel Turini8-Dec-04 4:00
Daniel Turini8-Dec-04 4:00 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Liu Shuai8-Dec-04 5:22
Liu Shuai8-Dec-04 5:22 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Mike Dimmick8-Dec-04 0:10
Mike Dimmick8-Dec-04 0:10 
GeneralRe: Problem with SendMessage() using P/Invoke Pin
Liu Shuai8-Dec-04 4:09
Liu Shuai8-Dec-04 4:09 
GeneralHELP ME!!! Pin
Jacob Graves7-Dec-04 13:44
Jacob Graves7-Dec-04 13:44 
GeneralRe: HELP ME!!! Pin
Heath Stewart7-Dec-04 14:51
protectorHeath Stewart7-Dec-04 14:51 
GeneralSorry.. Pin
Jacob Graves7-Dec-04 15:10
Jacob Graves7-Dec-04 15:10 
GeneralRe: Sorry.. Pin
Jitesh Patil7-Dec-04 21:53
Jitesh Patil7-Dec-04 21:53 
GeneralRe: Sorry.. Pin
Heath Stewart8-Dec-04 5:51
protectorHeath Stewart8-Dec-04 5:51 
GeneralThank you! Pin
Jacob Graves8-Dec-04 7:29
Jacob Graves8-Dec-04 7:29 
GeneralRe: Installing my program on other computers... Pin
Skynyrd7-Dec-04 13:21
Skynyrd7-Dec-04 13:21 
GeneralRe: Installing my program on other computers... Pin
Heath Stewart7-Dec-04 13:32
protectorHeath Stewart7-Dec-04 13:32 
GeneralRe: Installing my program on other computers... Pin
Moochie57-Dec-04 13:36
Moochie57-Dec-04 13:36 
GeneralRe: Installing my program on other computers... Pin
exhaulted7-Dec-04 23:18
exhaulted7-Dec-04 23:18 
GeneralRe: Installing my program on other computers... Pin
Daniel Turini8-Dec-04 1:38
Daniel Turini8-Dec-04 1:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.