C
Coder
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
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