PC Review


Reply
Thread Tools Rate Thread

Delegate and callback problem

 
 
Coder
Guest
Posts: n/a
 
      9th Sep 2003
Hello,



I'm converting a class from C# to VB .NET. The class handles or enforces
single instancing for a desktop PC application. Note this code is for the
..NET Framework not the Compact Framework.



I'm having trouble with 1 line of the converted code that involves a
delegate and a callback function.



Below I have included the working C# class and the converted VB .NET class
with a note (arrow) showing the line that is giving me trouble.



If you would be so kind, please try pasting the VB code into an empty VB
project to see the syntax error.



I think the problem is a conversion issue between C# and VB .NET



I hope you will find the working version of value.



Thank you.



C# Class ___________________________________________________



using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;

namespace SingleInstance
{
/// <summary>
/// Summary description for SingleApp.
/// </summary>
public class SingleApplication
{
public SingleApplication()
{

}
/// <summary>
/// Imports
/// </summary>

[DllImport("user32.Dll")]
private static extern int EnumWindows(EnumWinCallBack callBackFunc, int
lParam);

[DllImport("User32.Dll")]
private static extern void GetWindowText(int hWnd, StringBuilder str, int
nMaxCount);

[DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

/// <summary>
/// EnumWindowCallBack
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
private static bool EnumWindowCallBack(int hwnd, int lParam)
{
windowHandle = (IntPtr)hwnd;

StringBuilder sbuilder = new StringBuilder(256);
GetWindowText((int)windowHandle, sbuilder, sbuilder.Capacity);
string strTitle = sbuilder.ToString();

if(strTitle == sTitle)
{
ShowWindow(windowHandle, SW_RESTORE);
SetForegroundWindow(windowHandle);
return false;
}
return true;
}//EnumWindowCallBack

/// <summary>
/// Execute a form base application if another instance already running on
/// the system activate previous one
/// </summary>
/// <param name="frmMain">main form</param>
/// <returns>true if no previous instance is running</returns>
public static bool Run(System.Windows.Forms.Form frmMain)
{
if(IsAlreadyRunning())
{
sTitle = frmMain.Text;
//set focus on previously running app
EnumWindows (new EnumWinCallBack(EnumWindowCallBack), 0);
return false;
}
Application.Run(frmMain);
return true;
}

/// <summary>
/// for console base application
/// </summary>
/// <returns></returns>
public static bool Run()
{
if(IsAlreadyRunning())
{
return false;
}
return true;
}

/// <summary>
/// check if given exe alread running or not
/// </summary>
/// <returns>returns true if already running</returns>
private static bool IsAlreadyRunning()
{
string strLoc = Assembly.GetExecutingAssembly().Location;

FileSystemInfo fileInfo = new FileInfo(strLoc);
string sExeName = fileInfo.Name;
mutex = new Mutex(true, sExeName);

if (mutex.WaitOne(0, false))
{
return false;
}
return true;
}

static Mutex mutex;
const int SW_RESTORE = 9;
static string sTitle;
static IntPtr windowHandle;
delegate bool EnumWinCallBack(int hwnd, int lParam);
}
}


'___________________________________________________________________________



VB .NET Class ---



Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Diagnostics
Imports System.Threading
Imports System.Reflection
Imports System.IO

Namespace SingleInstance
Public Class SingleApplication
Shared Mutex As Mutex
Const SW_RESTORE As Integer = 9
Shared sTitle As String
Shared WindowHandle As Integer
Delegate Function EnumWinCallBack(ByVal hwnd As Integer, ByVal
lParam As Integer) As Boolean

<DllImport("user32.Dll")> Private Shared Function EnumWindows(ByVal
callBackFunc As EnumWinCallBack, ByVal lParam As Integer) As Integer
End Function
<DllImport("User32.Dll")> Private Shared Sub GetWindowText(ByVal
hWnd As Integer, ByRef str As StringBuilder, ByVal nMaxCount As Integer)
End Sub
<DllImport("user32.dll")> Private Shared Function
SetForegroundWindow(ByVal hWnd As Integer) As Boolean
End Function
<DllImport("user32.dll")> Private Shared Function ShowWindow(ByVal
hWnd As Integer, ByVal nCmdShow As Integer) As Boolean
End Function
Public Sub SingleApplication()

End Sub

Private Shared Function EnumWindowCallBack(ByVal hwnd As Integer,
ByVal lParam As Integer) As Boolean
WindowHandle = hwnd
Dim sBuilder As StringBuilder = New StringBuilder(256)

GetWindowText(WindowHandle, sBuilder, sBuilder.Capacity)
Dim strTitle As String = sBuilder.ToString

If strTitle = sTitle Then
ShowWindow(WindowHandle, SW_RESTORE)
Return False
End If
Return True
End Function

Public Shared Function Run(ByVal frmMain As
System.Windows.Forms.Form) As Boolean
If IsAlreadyRunning() Then
sTitle = frmMain.Text
'call this API function that takes a windows callback




syntax error here >>> EnumWindows(New
EnumWinCallBack(EnumWindowCallBack), 0) '<<<<Syntax error here <<<




Return False
Else
Application.Run(frmMain)
Return True
End If
End Function

Private Shared Function IsAlreadyRunning() As Boolean
Dim strLoc As String =
System.Reflection.Assembly.GetExecutingAssembly().Location
Dim fileInfo As FileSystemInfo = New FileInfo(strLoc)
Dim sExeName As String = fileInfo.Name
Mutex = New Mutex(True, sExeName)

If Mutex.WaitOne(0, False) Then
Return False
Else
Return True
End If
End Function

End Class
End Namespace




 
Reply With Quote
 
 
 
 
Coder
Guest
Posts: n/a
 
      9th Sep 2003
Also if you would like the project source code let me know and i will email
it to you.

This class works very well in C# Hopefully we can get the VB .NET version
working too.

Thanks

"Coder" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
>
>
> I'm converting a class from C# to VB .NET. The class handles or enforces
> single instancing for a desktop PC application. Note this code is for the
> .NET Framework not the Compact Framework.
>
>
>
> I'm having trouble with 1 line of the converted code that involves a
> delegate and a callback function.
>
>
>
> Below I have included the working C# class and the converted VB .NET class
> with a note (arrow) showing the line that is giving me trouble.
>
>
>
> If you would be so kind, please try pasting the VB code into an empty VB
> project to see the syntax error.
>
>
>
> I think the problem is a conversion issue between C# and VB .NET
>
>
>
> I hope you will find the working version of value.
>
>
>
> Thank you.
>
>
>
> C# Class ___________________________________________________
>
>
>
> using System;
> using System.Windows.Forms;
> using System.Runtime.InteropServices;
> using System.Text;
> using System.Diagnostics;
> using System.Threading;
> using System.Reflection;
> using System.IO;
>
> namespace SingleInstance
> {
> /// <summary>
> /// Summary description for SingleApp.
> /// </summary>
> public class SingleApplication
> {
> public SingleApplication()
> {
>
> }
> /// <summary>
> /// Imports
> /// </summary>
>
> [DllImport("user32.Dll")]
> private static extern int EnumWindows(EnumWinCallBack callBackFunc, int
> lParam);
>
> [DllImport("User32.Dll")]
> private static extern void GetWindowText(int hWnd, StringBuilder str,

int
> nMaxCount);
>
> [DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
> private static extern bool SetForegroundWindow(IntPtr hWnd);
>
> [DllImport("user32.dll")]
> private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);
>
> /// <summary>
> /// EnumWindowCallBack
> /// </summary>
> /// <param name="hwnd"></param>
> /// <param name="lParam"></param>
> /// <returns></returns>
> private static bool EnumWindowCallBack(int hwnd, int lParam)
> {
> windowHandle = (IntPtr)hwnd;
>
> StringBuilder sbuilder = new StringBuilder(256);
> GetWindowText((int)windowHandle, sbuilder, sbuilder.Capacity);
> string strTitle = sbuilder.ToString();
>
> if(strTitle == sTitle)
> {
> ShowWindow(windowHandle, SW_RESTORE);
> SetForegroundWindow(windowHandle);
> return false;
> }
> return true;
> }//EnumWindowCallBack
>
> /// <summary>
> /// Execute a form base application if another instance already running

on
> /// the system activate previous one
> /// </summary>
> /// <param name="frmMain">main form</param>
> /// <returns>true if no previous instance is running</returns>
> public static bool Run(System.Windows.Forms.Form frmMain)
> {
> if(IsAlreadyRunning())
> {
> sTitle = frmMain.Text;
> //set focus on previously running app
> EnumWindows (new EnumWinCallBack(EnumWindowCallBack), 0);
> return false;
> }
> Application.Run(frmMain);
> return true;
> }
>
> /// <summary>
> /// for console base application
> /// </summary>
> /// <returns></returns>
> public static bool Run()
> {
> if(IsAlreadyRunning())
> {
> return false;
> }
> return true;
> }
>
> /// <summary>
> /// check if given exe alread running or not
> /// </summary>
> /// <returns>returns true if already running</returns>
> private static bool IsAlreadyRunning()
> {
> string strLoc = Assembly.GetExecutingAssembly().Location;
>
> FileSystemInfo fileInfo = new FileInfo(strLoc);
> string sExeName = fileInfo.Name;
> mutex = new Mutex(true, sExeName);
>
> if (mutex.WaitOne(0, false))
> {
> return false;
> }
> return true;
> }
>
> static Mutex mutex;
> const int SW_RESTORE = 9;
> static string sTitle;
> static IntPtr windowHandle;
> delegate bool EnumWinCallBack(int hwnd, int lParam);
> }
> }
>
>
>

'___________________________________________________________________________
>
>
>
> VB .NET Class ---
>
>
>
> Imports System
> Imports System.Windows.Forms
> Imports System.Runtime.InteropServices
> Imports System.Text
> Imports System.Diagnostics
> Imports System.Threading
> Imports System.Reflection
> Imports System.IO
>
> Namespace SingleInstance
> Public Class SingleApplication
> Shared Mutex As Mutex
> Const SW_RESTORE As Integer = 9
> Shared sTitle As String
> Shared WindowHandle As Integer
> Delegate Function EnumWinCallBack(ByVal hwnd As Integer, ByVal
> lParam As Integer) As Boolean
>
> <DllImport("user32.Dll")> Private Shared Function

EnumWindows(ByVal
> callBackFunc As EnumWinCallBack, ByVal lParam As Integer) As Integer
> End Function
> <DllImport("User32.Dll")> Private Shared Sub GetWindowText(ByVal
> hWnd As Integer, ByRef str As StringBuilder, ByVal nMaxCount As Integer)
> End Sub
> <DllImport("user32.dll")> Private Shared Function
> SetForegroundWindow(ByVal hWnd As Integer) As Boolean
> End Function
> <DllImport("user32.dll")> Private Shared Function ShowWindow(ByVal
> hWnd As Integer, ByVal nCmdShow As Integer) As Boolean
> End Function
> Public Sub SingleApplication()
>
> End Sub
>
> Private Shared Function EnumWindowCallBack(ByVal hwnd As Integer,
> ByVal lParam As Integer) As Boolean
> WindowHandle = hwnd
> Dim sBuilder As StringBuilder = New StringBuilder(256)
>
> GetWindowText(WindowHandle, sBuilder, sBuilder.Capacity)
> Dim strTitle As String = sBuilder.ToString
>
> If strTitle = sTitle Then
> ShowWindow(WindowHandle, SW_RESTORE)
> Return False
> End If
> Return True
> End Function
>
> Public Shared Function Run(ByVal frmMain As
> System.Windows.Forms.Form) As Boolean
> If IsAlreadyRunning() Then
> sTitle = frmMain.Text
> 'call this API function that takes a windows callback
>
>
>
>
> syntax error here >>> EnumWindows(New
> EnumWinCallBack(EnumWindowCallBack), 0) '<<<<Syntax error here <<<
>
>
>
>
> Return False
> Else
> Application.Run(frmMain)
> Return True
> End If
> End Function
>
> Private Shared Function IsAlreadyRunning() As Boolean
> Dim strLoc As String =
> System.Reflection.Assembly.GetExecutingAssembly().Location
> Dim fileInfo As FileSystemInfo = New FileInfo(strLoc)
> Dim sExeName As String = fileInfo.Name
> Mutex = New Mutex(True, sExeName)
>
> If Mutex.WaitOne(0, False) Then
> Return False
> Else
> Return True
> End If
> End Function
>
> End Class
> End Namespace
>
>
>
>



 
Reply With Quote
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      9th Sep 2003
You need to use AddressOf operator

EnumWindows(New EnumWinCallBack(AddressOf EnumWindowCallBack), 0)

"Coder" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Also if you would like the project source code let me know and i will

email
> it to you.
>
> This class works very well in C# Hopefully we can get the VB .NET version
> working too.
>
> Thanks
>
> "Coder" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hello,
> >
> >
> >
> > I'm converting a class from C# to VB .NET. The class handles or enforces
> > single instancing for a desktop PC application. Note this code is for

the
> > .NET Framework not the Compact Framework.
> >
> >
> >
> > I'm having trouble with 1 line of the converted code that involves a
> > delegate and a callback function.
> >
> >
> >
> > Below I have included the working C# class and the converted VB .NET

class
> > with a note (arrow) showing the line that is giving me trouble.
> >
> >
> >
> > If you would be so kind, please try pasting the VB code into an empty VB
> > project to see the syntax error.
> >
> >
> >
> > I think the problem is a conversion issue between C# and VB .NET
> >
> >
> >
> > I hope you will find the working version of value.
> >
> >
> >
> > Thank you.
> >
> >
> >
> > C# Class ___________________________________________________
> >
> >
> >
> > using System;
> > using System.Windows.Forms;
> > using System.Runtime.InteropServices;
> > using System.Text;
> > using System.Diagnostics;
> > using System.Threading;
> > using System.Reflection;
> > using System.IO;
> >
> > namespace SingleInstance
> > {
> > /// <summary>
> > /// Summary description for SingleApp.
> > /// </summary>
> > public class SingleApplication
> > {
> > public SingleApplication()
> > {
> >
> > }
> > /// <summary>
> > /// Imports
> > /// </summary>
> >
> > [DllImport("user32.Dll")]
> > private static extern int EnumWindows(EnumWinCallBack callBackFunc,

int
> > lParam);
> >
> > [DllImport("User32.Dll")]
> > private static extern void GetWindowText(int hWnd, StringBuilder str,

> int
> > nMaxCount);
> >
> > [DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
> > private static extern bool SetForegroundWindow(IntPtr hWnd);
> >
> > [DllImport("user32.dll")]
> > private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);
> >
> > /// <summary>
> > /// EnumWindowCallBack
> > /// </summary>
> > /// <param name="hwnd"></param>
> > /// <param name="lParam"></param>
> > /// <returns></returns>
> > private static bool EnumWindowCallBack(int hwnd, int lParam)
> > {
> > windowHandle = (IntPtr)hwnd;
> >
> > StringBuilder sbuilder = new StringBuilder(256);
> > GetWindowText((int)windowHandle, sbuilder, sbuilder.Capacity);
> > string strTitle = sbuilder.ToString();
> >
> > if(strTitle == sTitle)
> > {
> > ShowWindow(windowHandle, SW_RESTORE);
> > SetForegroundWindow(windowHandle);
> > return false;
> > }
> > return true;
> > }//EnumWindowCallBack
> >
> > /// <summary>
> > /// Execute a form base application if another instance already

running
> on
> > /// the system activate previous one
> > /// </summary>
> > /// <param name="frmMain">main form</param>
> > /// <returns>true if no previous instance is running</returns>
> > public static bool Run(System.Windows.Forms.Form frmMain)
> > {
> > if(IsAlreadyRunning())
> > {
> > sTitle = frmMain.Text;
> > //set focus on previously running app
> > EnumWindows (new EnumWinCallBack(EnumWindowCallBack), 0);
> > return false;
> > }
> > Application.Run(frmMain);
> > return true;
> > }
> >
> > /// <summary>
> > /// for console base application
> > /// </summary>
> > /// <returns></returns>
> > public static bool Run()
> > {
> > if(IsAlreadyRunning())
> > {
> > return false;
> > }
> > return true;
> > }
> >
> > /// <summary>
> > /// check if given exe alread running or not
> > /// </summary>
> > /// <returns>returns true if already running</returns>
> > private static bool IsAlreadyRunning()
> > {
> > string strLoc = Assembly.GetExecutingAssembly().Location;
> >
> > FileSystemInfo fileInfo = new FileInfo(strLoc);
> > string sExeName = fileInfo.Name;
> > mutex = new Mutex(true, sExeName);
> >
> > if (mutex.WaitOne(0, false))
> > {
> > return false;
> > }
> > return true;
> > }
> >
> > static Mutex mutex;
> > const int SW_RESTORE = 9;
> > static string sTitle;
> > static IntPtr windowHandle;
> > delegate bool EnumWinCallBack(int hwnd, int lParam);
> > }
> > }
> >
> >
> >

>

'___________________________________________________________________________
> >
> >
> >
> > VB .NET Class ---
> >
> >
> >
> > Imports System
> > Imports System.Windows.Forms
> > Imports System.Runtime.InteropServices
> > Imports System.Text
> > Imports System.Diagnostics
> > Imports System.Threading
> > Imports System.Reflection
> > Imports System.IO
> >
> > Namespace SingleInstance
> > Public Class SingleApplication
> > Shared Mutex As Mutex
> > Const SW_RESTORE As Integer = 9
> > Shared sTitle As String
> > Shared WindowHandle As Integer
> > Delegate Function EnumWinCallBack(ByVal hwnd As Integer, ByVal
> > lParam As Integer) As Boolean
> >
> > <DllImport("user32.Dll")> Private Shared Function

> EnumWindows(ByVal
> > callBackFunc As EnumWinCallBack, ByVal lParam As Integer) As Integer
> > End Function
> > <DllImport("User32.Dll")> Private Shared Sub GetWindowText(ByVal
> > hWnd As Integer, ByRef str As StringBuilder, ByVal nMaxCount As Integer)
> > End Sub
> > <DllImport("user32.dll")> Private Shared Function
> > SetForegroundWindow(ByVal hWnd As Integer) As Boolean
> > End Function
> > <DllImport("user32.dll")> Private Shared Function

ShowWindow(ByVal
> > hWnd As Integer, ByVal nCmdShow As Integer) As Boolean
> > End Function
> > Public Sub SingleApplication()
> >
> > End Sub
> >
> > Private Shared Function EnumWindowCallBack(ByVal hwnd As

Integer,
> > ByVal lParam As Integer) As Boolean
> > WindowHandle = hwnd
> > Dim sBuilder As StringBuilder = New StringBuilder(256)
> >
> > GetWindowText(WindowHandle, sBuilder, sBuilder.Capacity)
> > Dim strTitle As String = sBuilder.ToString
> >
> > If strTitle = sTitle Then
> > ShowWindow(WindowHandle, SW_RESTORE)
> > Return False
> > End If
> > Return True
> > End Function
> >
> > Public Shared Function Run(ByVal frmMain As
> > System.Windows.Forms.Form) As Boolean
> > If IsAlreadyRunning() Then
> > sTitle = frmMain.Text
> > 'call this API function that takes a windows callback
> >
> >
> >
> >
> > syntax error here >>> EnumWindows(New
> > EnumWinCallBack(EnumWindowCallBack), 0) '<<<<Syntax error here <<<
> >
> >
> >
> >
> > Return False
> > Else
> > Application.Run(frmMain)
> > Return True
> > End If
> > End Function
> >
> > Private Shared Function IsAlreadyRunning() As Boolean
> > Dim strLoc As String =
> > System.Reflection.Assembly.GetExecutingAssembly().Location
> > Dim fileInfo As FileSystemInfo = New FileInfo(strLoc)
> > Dim sExeName As String = fileInfo.Name
> > Mutex = New Mutex(True, sExeName)
> >
> > If Mutex.WaitOne(0, False) Then
> > Return False
> > Else
> > Return True
> > End If
> > End Function
> >
> > End Class
> > End Namespace
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Coder
Guest
Posts: n/a
 
      9th Sep 2003
i tried that before but it didn't work... So i tried it again as you said
and it didnt work, at first.... Then after several seconds Visual Studio
took off the syntax error and underline highlight. So it worked the first
time but VS was slow to update the line. This has bitten me before. is there
a problem with the speed of syntax checking in VS. I run a P4 2.4 Ghz dev
system. Maybe i should just turn off syntax checking



"Alex Feinman [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> You need to use AddressOf operator
>
> EnumWindows(New EnumWinCallBack(AddressOf EnumWindowCallBack), 0)
>
> "Coder" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Also if you would like the project source code let me know and i will

> email
> > it to you.
> >
> > This class works very well in C# Hopefully we can get the VB .NET

version
> > working too.
> >
> > Thanks
> >
> > "Coder" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Hello,
> > >
> > >
> > >
> > > I'm converting a class from C# to VB .NET. The class handles or

enforces
> > > single instancing for a desktop PC application. Note this code is for

> the
> > > .NET Framework not the Compact Framework.
> > >
> > >
> > >
> > > I'm having trouble with 1 line of the converted code that involves a
> > > delegate and a callback function.
> > >
> > >
> > >
> > > Below I have included the working C# class and the converted VB .NET

> class
> > > with a note (arrow) showing the line that is giving me trouble.
> > >
> > >
> > >
> > > If you would be so kind, please try pasting the VB code into an empty

VB
> > > project to see the syntax error.
> > >
> > >
> > >
> > > I think the problem is a conversion issue between C# and VB .NET
> > >
> > >
> > >
> > > I hope you will find the working version of value.
> > >
> > >
> > >
> > > Thank you.
> > >
> > >
> > >
> > > C# Class ___________________________________________________
> > >
> > >
> > >
> > > using System;
> > > using System.Windows.Forms;
> > > using System.Runtime.InteropServices;
> > > using System.Text;
> > > using System.Diagnostics;
> > > using System.Threading;
> > > using System.Reflection;
> > > using System.IO;
> > >
> > > namespace SingleInstance
> > > {
> > > /// <summary>
> > > /// Summary description for SingleApp.
> > > /// </summary>
> > > public class SingleApplication
> > > {
> > > public SingleApplication()
> > > {
> > >
> > > }
> > > /// <summary>
> > > /// Imports
> > > /// </summary>
> > >
> > > [DllImport("user32.Dll")]
> > > private static extern int EnumWindows(EnumWinCallBack callBackFunc,

> int
> > > lParam);
> > >
> > > [DllImport("User32.Dll")]
> > > private static extern void GetWindowText(int hWnd, StringBuilder

str,
> > int
> > > nMaxCount);
> > >
> > > [DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
> > > private static extern bool SetForegroundWindow(IntPtr hWnd);
> > >
> > > [DllImport("user32.dll")]
> > > private static extern Boolean ShowWindow(IntPtr hWnd,Int32

nCmdShow);
> > >
> > > /// <summary>
> > > /// EnumWindowCallBack
> > > /// </summary>
> > > /// <param name="hwnd"></param>
> > > /// <param name="lParam"></param>
> > > /// <returns></returns>
> > > private static bool EnumWindowCallBack(int hwnd, int lParam)
> > > {
> > > windowHandle = (IntPtr)hwnd;
> > >
> > > StringBuilder sbuilder = new StringBuilder(256);
> > > GetWindowText((int)windowHandle, sbuilder, sbuilder.Capacity);
> > > string strTitle = sbuilder.ToString();
> > >
> > > if(strTitle == sTitle)
> > > {
> > > ShowWindow(windowHandle, SW_RESTORE);
> > > SetForegroundWindow(windowHandle);
> > > return false;
> > > }
> > > return true;
> > > }//EnumWindowCallBack
> > >
> > > /// <summary>
> > > /// Execute a form base application if another instance already

> running
> > on
> > > /// the system activate previous one
> > > /// </summary>
> > > /// <param name="frmMain">main form</param>
> > > /// <returns>true if no previous instance is running</returns>
> > > public static bool Run(System.Windows.Forms.Form frmMain)
> > > {
> > > if(IsAlreadyRunning())
> > > {
> > > sTitle = frmMain.Text;
> > > //set focus on previously running app
> > > EnumWindows (new EnumWinCallBack(EnumWindowCallBack), 0);
> > > return false;
> > > }
> > > Application.Run(frmMain);
> > > return true;
> > > }
> > >
> > > /// <summary>
> > > /// for console base application
> > > /// </summary>
> > > /// <returns></returns>
> > > public static bool Run()
> > > {
> > > if(IsAlreadyRunning())
> > > {
> > > return false;
> > > }
> > > return true;
> > > }
> > >
> > > /// <summary>
> > > /// check if given exe alread running or not
> > > /// </summary>
> > > /// <returns>returns true if already running</returns>
> > > private static bool IsAlreadyRunning()
> > > {
> > > string strLoc = Assembly.GetExecutingAssembly().Location;
> > >
> > > FileSystemInfo fileInfo = new FileInfo(strLoc);
> > > string sExeName = fileInfo.Name;
> > > mutex = new Mutex(true, sExeName);
> > >
> > > if (mutex.WaitOne(0, false))
> > > {
> > > return false;
> > > }
> > > return true;
> > > }
> > >
> > > static Mutex mutex;
> > > const int SW_RESTORE = 9;
> > > static string sTitle;
> > > static IntPtr windowHandle;
> > > delegate bool EnumWinCallBack(int hwnd, int lParam);
> > > }
> > > }
> > >
> > >
> > >

> >

>

'___________________________________________________________________________
> > >
> > >
> > >
> > > VB .NET Class ---
> > >
> > >
> > >
> > > Imports System
> > > Imports System.Windows.Forms
> > > Imports System.Runtime.InteropServices
> > > Imports System.Text
> > > Imports System.Diagnostics
> > > Imports System.Threading
> > > Imports System.Reflection
> > > Imports System.IO
> > >
> > > Namespace SingleInstance
> > > Public Class SingleApplication
> > > Shared Mutex As Mutex
> > > Const SW_RESTORE As Integer = 9
> > > Shared sTitle As String
> > > Shared WindowHandle As Integer
> > > Delegate Function EnumWinCallBack(ByVal hwnd As Integer, ByVal
> > > lParam As Integer) As Boolean
> > >
> > > <DllImport("user32.Dll")> Private Shared Function

> > EnumWindows(ByVal
> > > callBackFunc As EnumWinCallBack, ByVal lParam As Integer) As Integer
> > > End Function
> > > <DllImport("User32.Dll")> Private Shared Sub

GetWindowText(ByVal
> > > hWnd As Integer, ByRef str As StringBuilder, ByVal nMaxCount As

Integer)
> > > End Sub
> > > <DllImport("user32.dll")> Private Shared Function
> > > SetForegroundWindow(ByVal hWnd As Integer) As Boolean
> > > End Function
> > > <DllImport("user32.dll")> Private Shared Function

> ShowWindow(ByVal
> > > hWnd As Integer, ByVal nCmdShow As Integer) As Boolean
> > > End Function
> > > Public Sub SingleApplication()
> > >
> > > End Sub
> > >
> > > Private Shared Function EnumWindowCallBack(ByVal hwnd As

> Integer,
> > > ByVal lParam As Integer) As Boolean
> > > WindowHandle = hwnd
> > > Dim sBuilder As StringBuilder = New StringBuilder(256)
> > >
> > > GetWindowText(WindowHandle, sBuilder, sBuilder.Capacity)
> > > Dim strTitle As String = sBuilder.ToString
> > >
> > > If strTitle = sTitle Then
> > > ShowWindow(WindowHandle, SW_RESTORE)
> > > Return False
> > > End If
> > > Return True
> > > End Function
> > >
> > > Public Shared Function Run(ByVal frmMain As
> > > System.Windows.Forms.Form) As Boolean
> > > If IsAlreadyRunning() Then
> > > sTitle = frmMain.Text
> > > 'call this API function that takes a windows callback
> > >
> > >
> > >
> > >
> > > syntax error here >>> EnumWindows(New
> > > EnumWinCallBack(EnumWindowCallBack), 0) '<<<<Syntax error here <<<
> > >
> > >
> > >
> > >
> > > Return False
> > > Else
> > > Application.Run(frmMain)
> > > Return True
> > > End If
> > > End Function
> > >
> > > Private Shared Function IsAlreadyRunning() As Boolean
> > > Dim strLoc As String =
> > > System.Reflection.Assembly.GetExecutingAssembly().Location
> > > Dim fileInfo As FileSystemInfo = New FileInfo(strLoc)
> > > Dim sExeName As String = fileInfo.Name
> > > Mutex = New Mutex(True, sExeName)
> > >
> > > If Mutex.WaitOne(0, False) Then
> > > Return False
> > > Else
> > > Return True
> > > End If
> > > End Function
> > >
> > > End Class
> > > End Namespace
> > >
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      9th Sep 2003
"Coder" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> i tried that before but it didn't work... So i tried it again as you said
> and it didnt work, at first.... Then after several seconds Visual Studio
> took off the syntax error and underline highlight. So it worked the first
> time but VS was slow to update the line. This has bitten me before. is

there
> a problem with the speed of syntax checking in VS. I run a P4 2.4 Ghz dev
> system. Maybe i should just turn off syntax checking


Try closing the Dynamic Help window


 
Reply With Quote
 
Coder
Guest
Posts: n/a
 
      9th Sep 2003
Now that I have the instancing class converted im having trouble converting
the C# form code to VB .NET to use the class.

The C# form code is below. How can i use the instancing class with a VB
startup form? any ideas?


Thanks

C# Form code ____________________________________________________________

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;

namespace TestApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class FrmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblMsg;
private System.Windows.Forms.Button btnClose;

/// <summary>
///
/// </summary>
public FrmMain()
{
// Required for Windows Form Designer support
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblMsg = new System.Windows.Forms.Label();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblMsg
//
this.lblMsg.Location = new System.Drawing.Point(40, 40);
this.lblMsg.Name = "lblMsg";
this.lblMsg.Size = new System.Drawing.Size(208, 16);
this.lblMsg.TabIndex = 2;
this.lblMsg.Text = "Single Application Instance Demo";
//
// btnClose
//
this.btnClose.Anchor =
((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bott
om | System.Windows.Forms.AnchorStyles.Right)));
this.btnClose.Location = new System.Drawing.Point(192, 104);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 1;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// FrmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 141);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.lblMsg);
this.Name = "FrmMain";
this.Text = "SingleInstance";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
SingleInstance.SingleApplication.Run(new FrmMain());
}

private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
}
}

}


"Alex Feinman [MVP]" <(E-Mail Removed)> wrote in message
news:%23G$$(E-Mail Removed)...
> "Coder" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > i tried that before but it didn't work... So i tried it again as you

said
> > and it didnt work, at first.... Then after several seconds Visual Studio
> > took off the syntax error and underline highlight. So it worked the

first
> > time but VS was slow to update the line. This has bitten me before. is

> there
> > a problem with the speed of syntax checking in VS. I run a P4 2.4 Ghz

dev
> > system. Maybe i should just turn off syntax checking

>
> Try closing the Dynamic Help window
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with delegate callback error Flomo Togba Kwele Microsoft VB .NET 9 7th May 2007 04:37 AM
write delegate & callback in dll James Wong Microsoft Dot NET 5 28th Jul 2006 11:20 AM
delegate for unmanaged callback =?Utf-8?B?dGIyMDAw?= Microsoft Dot NET Compact Framework 5 10th Feb 2006 06:10 AM
callback Proc: need delegate obj. Pls help suria` Microsoft VB .NET 2 24th Nov 2003 09:02 AM
Delegate and callback problem Coder Microsoft Dot NET Framework 5 9th Sep 2003 07:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:05 AM.