Home All Groups Group Topic Archive Search About

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

Author
26 May 2006 4:31 PM
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

Author
26 May 2006 4:29 PM
Mythran
Show quote Hide quote
"TechN00b" <n***@noone.where.net> wrote in message
news:0SFdg.60339$u13.45601@fe43.usenetserver.com...
> 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
Author
26 May 2006 4:58 PM
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.


Show quoteHide quote
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
news:OQLbbGOgGHA.1856@TK2MSFTNGP03.phx.gbl...
>
> "TechN00b" <n***@noone.where.net> wrote in message
> news:0SFdg.60339$u13.45601@fe43.usenetserver.com...
> > 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
>
Author
26 May 2006 4:53 PM
Mythran
Show quote Hide quote
"TechN00b" <n***@noone.where.net> wrote in message
news:ZeGdg.78597$mJ3.28810@fe24.usenetserver.com...
> 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.
>
>
> "Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
> news:OQLbbGOgGHA.1856@TK2MSFTNGP03.phx.gbl...
>>
>> "TechN00b" <n***@noone.where.net> wrote in message
>> news:0SFdg.60339$u13.45601@fe43.usenetserver.com...
>> > 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
>>
>
>
>

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
Author
26 May 2006 5:22 PM
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.


Show quoteHide quote
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
news:%23ePgqTOgGHA.4976@TK2MSFTNGP02.phx.gbl...
>
> "TechN00b" <n***@noone.where.net> wrote in message
> news:ZeGdg.78597$mJ3.28810@fe24.usenetserver.com...
> > 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.
> >
> >
> > "Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
> > news:OQLbbGOgGHA.1856@TK2MSFTNGP03.phx.gbl...
> >>
> >> "TechN00b" <n***@noone.where.net> wrote in message
> >> news:0SFdg.60339$u13.45601@fe43.usenetserver.com...
> >> > 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
> >>
> >
> >
> >
>
> 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
>
Author
26 May 2006 6:30 PM
Herfried K. Wagner [MVP]
"TechN00b" <n***@noone.where.net> schrieb:
> 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'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
26 May 2006 4:52 PM
Jim Wooley
Show quote Hide quote
> 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
Author
26 May 2006 5:31 PM
TechN00b
That looks like it worked!  Thanks for the explanation as well, I appreciate
the help.
Thanks Jim!
Thanks everyone!

Show quoteHide quote
"Jim Wooley" <jimNOSPAMwooley@hotmail.com> wrote in message
news:24f81e8ee8aa8c84eda72fb1b17@msnews.microsoft.com...
> > 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))
Show quoteHide quote
>         Next
>         Console.ReadLine()
>     End Sub
> End Class
>
> Jim Wooley
> http://devauthority.com/blogs/jwooley/default.aspx
>
>
Author
26 May 2006 6:28 PM
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
Author
26 May 2006 5:45 PM
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