Console app no accessible 'Main' method with an app...

M

Mythran

TechN00b said:
I'm trying to write a quick commandline app that takes a string from the
commandline and returns a formatted md5 hash. Unfortunately the code
won't
comple and returns an error of "No accessible 'Main' method with an
appropriate signature was found"

Anyone have an idea what I need to do to fix this?

Thanks!

Full code follows:
-----------------------------------
Imports System
Imports System.Text
Imports System.Security.Cryptography
Module MD5Return

Function MD5hash(ByVal data() As Byte) As Byte()
' This is one implementation of the abstract class MD5.
Dim md5 As New MD5CryptoServiceProvider()
Dim result As Byte() = md5.ComputeHash(data)
Return result
End Function

Public Sub Main(ByVal args As Char())
Dim clearBytes, hashedBytes As Byte()
If (args Is Nothing OrElse args.Length = 0) Then
clearBytes = New UnicodeEncoding().GetBytes(args)
hashedBytes = MD5hash(clearBytes)
Console.Write(BitConverter.ToString(hashedBytes))
End If
End Sub
End Module

You Public Sub Main should read:

Public Shared Sub Main(ByVal args As String())
...
End Sub

Maybe even prepend it with the STAThread attribute too :)

Mythran
 
T

TechN00b

I'm trying to write a quick commandline app that takes a string from the
commandline and returns a formatted md5 hash. Unfortunately the code won't
comple and returns an error of "No accessible 'Main' method with an
appropriate signature was found"

Anyone have an idea what I need to do to fix this?

Thanks!

Full code follows:
-----------------------------------
Imports System
Imports System.Text
Imports System.Security.Cryptography
Module MD5Return

Function MD5hash(ByVal data() As Byte) As Byte()
' This is one implementation of the abstract class MD5.
Dim md5 As New MD5CryptoServiceProvider()
Dim result As Byte() = md5.ComputeHash(data)
Return result
End Function

Public Sub Main(ByVal args As Char())
Dim clearBytes, hashedBytes As Byte()
If (args Is Nothing OrElse args.Length = 0) Then
clearBytes = New UnicodeEncoding().GetBytes(args)
hashedBytes = MD5hash(clearBytes)
Console.Write(BitConverter.ToString(hashedBytes))
End If
End Sub
End Module
 
J

Jim Wooley

I'm trying to write a quick commandline app that takes a string from
the commandline and returns a formatted md5 hash. Unfortunately the
code won't comple and returns an error of "No accessible 'Main' method
with an appropriate signature was found"

Anyone have an idea what I need to do to fix this?

Thanks!

Full code follows:
-----------------------------------
Imports System
Imports System.Text
Imports System.Security.Cryptography
Module MD5Return
Function MD5hash(ByVal data() As Byte) As Byte()
' This is one implementation of the abstract class MD5.
Dim md5 As New MD5CryptoServiceProvider()
Dim result As Byte() = md5.ComputeHash(data)
Return result
End Function
Public Sub Main(ByVal args As Char())
Dim clearBytes, hashedBytes As Byte()
If (args Is Nothing OrElse args.Length = 0) Then
clearBytes = New UnicodeEncoding().GetBytes(args)
hashedBytes = MD5hash(clearBytes)
Console.Write(BitConverter.ToString(hashedBytes))
End If
End Sub
End Module

The Sub Main should be static and not take parameters. Additionally, it should
be inside of a class. Since we change sub Main to shared, it can no longer
directly call MD5hash as it requires an instance. Thus, we can make that
method shard as well. The command line args can be retrieved using Environment.GetCommandLineArgs().
Below is a re-write of your sample. See if it helps.

Imports System
Imports System.Text
Imports System.Security.Cryptography
Public Class MD5Return

Private Shared Function MD5hash(ByVal data() As Byte) As Byte()
' This is one implementation of the abstract class MD5.
Dim md5 As New MD5CryptoServiceProvider()
Dim result As Byte() = md5.ComputeHash(data)
Return result
End Function

Public Shared Sub Main()
Dim clearBytes, hashedBytes As Byte()
Dim args As String() = Environment.GetCommandLineArgs
For Each arg As String In args
clearBytes = New UnicodeEncoding().GetBytes(arg)
hashedBytes = MD5hash(clearBytes)
Console.WriteLine(arg & "= " & BitConverter.ToString(hashedBytes))
Next
Console.ReadLine()
End Sub
End Class

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
M

Mythran

TechN00b said:
Thanks for the quick response. when I change from
Public Sub Main(ByVal args As Char())
to
Public Shared Sub Main(ByVal args As Char())
it tells me that 'Methods in a module cannot be declared 'Shared''

I'm trying this in VisualStudio 2005, if that makes any difference.

Doh! I didn't see it was a module...I don't have VS2k5, but what you can do
to possibly fix this is check the project options screen and under the Build
or Debug or General tab should be a field or set of fields that define the
main entry point or how to start the application. You may try looking for
this...(in VS2k3 it was under the General tab, field was called Startup
object).

Ya know what sucks? I'm not allowed to switch to VS2k5 until
about...umm...2010 :p No, later this year...I hate that we aren't on top of
the tech. train, we seem to be somewhere trailing the caboose.

HTH,
Mythran
 
T

TechN00b

Thanks for the quick response. when I change from
Public Sub Main(ByVal args As Char())
to
Public Shared Sub Main(ByVal args As Char())
it tells me that 'Methods in a module cannot be declared 'Shared''

I'm trying this in VisualStudio 2005, if that makes any difference.
 
T

TechN00b

I am unfortunately very inexperienced in VS.NET, it there a way to to this
that isn't a module? I'll put 2k3 on this machine if it helps. :-D
What's happening is that there's an application I need to interface with
that has the following as the encryprion for the password. I would like to
interface with the application using PHP, but php doesn't have an
Unicode_Encode function working yet so I figured I would just make a quick
vb console app to take a string as a command line argument and output the
hash to the console, which I can grab through php.

public static string Encrypt(string cleanString)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString);
Byte[] hashedBytes = ((HashAlgorithm)
CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}

Thanks for your help, I appreciate it.
 
T

TechN00b

That looks like it worked! Thanks for the explanation as well, I appreciate
the help.
Thanks Jim!
Thanks everyone!
 
M

Mythran

The Sub Main should be static and not take parameters. Additionally, it
should be inside of a class. Since we change sub Main to shared, it can no
longer directly call MD5hash as it requires an instance. Thus, we can make
that method shard as well. The command line args can be retrieved using
Environment.GetCommandLineArgs(). Below is a re-write of your sample. See
if it helps.

VB.Net 2k5 changed that much? I mean, you can't have the following work in
a class for a console application?

Public Shared Function Main(ByVal Args As String()) As Integer
For Each arg As String In Args
Console.WriteLine(arg)
Next
Return 0
End Function

Mythran
 
M

Mythran

An alternate way of doing this, using a very similar layout the OP
originally used, is as follows:

Imports System
Imports System.Text
Imports System.Security.Cryptography

Module MD5Return

#Region " Public Methods "
''' <summary>
''' The main entry point for the application.
''' </summary>
''' <param name="Args">
''' Array of command-line arguments.
''' </param>
Public Sub Main(ByVal Args As String())
If Args.Length > 0
Dim input As String = String.Join(" ", Args)
Dim clearBytes As Byte() = _
New UnicodeEncoding().GetBytes(input)
Dim hashedBytes As Byte() = _
(New MD5CryptoServiceProvider()).ComputeHash(clearBytes)
Console.Write(BitConverter.ToString(hashedBytes))
End If
End Sub
#End Region

End Module


The reason it was failing originally was because the Main method requires
either no arguments, or a string-array ... not a character-array. Also,
make sure your Module name is the same as the file it's contained in
(otherwise, Visual Studio complains that it can't find the appropriate Main
method).

Note: removed the MD5hash method and replaced with the inline equivalent.
Replaced Char() argument with String(). Used String.Join() to join the
array into a space-delimited string.

HTH :)

Mythran
 
H

Herfried K. Wagner [MVP]

TechN00b said:
Thanks for the quick response. when I change from
Public Sub Main(ByVal args As Char())
to
Public Shared Sub Main(ByVal args As Char())
it tells me that 'Methods in a module cannot be declared 'Shared''

I'm trying this in VisualStudio 2005, if that makes any difference.

Simply remove the 'Shared' keyword and change 'ByVal args As Char()' to
'ByVal Args() As String'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top