‘AddressOf’ expression cannot be converted to ‘Integer’

MadMaxMSCN

Newcomer
Joined
Aug 18, 2004
Messages
23
Location
Bavaria, Germany
Hi

i have to ugrade a vb6-program to vb.net
there's one remaining problem i have:

the upgradewizard says:
'UPGRADE_WARNING: Add a delegate for AddressOf DLLPrnt
MYUSER.DLLPrnt = FnPtr(AddressOf DLLPrnt)


here's my DLLPrnt:
Function DLLPrnt(ByRef fname As CBChar, ByVal x As Integer) As Integer

and here my FnPtr:
' Puts a function pointer in a structure
Function FnPtr(ByVal lp As Integer) As Integer
FnPtr = lp
End Function


i tried to add a delegate, like shown in msdn and on other web pages,
for example here:
www.elitevb.com/content/01,0075/04.aspx

unfortunatly the only result was:
‘AddressOf’ expression cannot be converted to ‘Integer’ because ‘Integer’ is not a delegate type.

whats wrong, how does it work?

thanks for helping
max
 
PlausiblyDamp said:
could you post the code in question - including the bit where you try to add a delegate

After running the upgrade wizard (without changing any code myself)
I get this message:
‘AddressOf’ expression cannot be converted to ‘Integer’ because ‘Integer’ is not a delegate type.

for the line:
MYUSER.DLLPrnt = FnPtr(AddressOf DLLPrnt)

the line before is a upgrade warning created by the wizard:
'UPGRADE_WARNING: Add a delegate for AddressOf DLLPrnt

the Function FnPtr you can see in my last post,
here is my complete DLLPrnt (without changing any code myself):
Function DLLPrnt(ByRef fname As CBChar, ByVal x As Integer) As Integer
Dim s0 As String
Dim xx As Integer
Dim sVbZipInf As String

' always put this in callback routines!
On Error Resume Next
s0 = ""
For xx = 0 To x
If fname.ch(xx) = 0 Then xx = 99999 Else s0 = s0 & Chr(fname.ch(xx))
Next xx

System.Diagnostics.Debug.WriteLine(sVbZipInf & s0)
msOutput = msOutput & s0

sVbZipInf = ""

System.Windows.Forms.Application.DoEvents()
DLLPrnt = 0

End Function



after my try to add a delegate it looks so:

Delegate Function DLLPrntDelegate(ByRef fname As CBChar)

Function DLLPrnt(ByRef fname As CBChar, ByVal x As DLLPrntDelegate)
Dim s0 As String
Dim xx As Integer
Dim sVbZipInf As String

' always put this in callback routines!
On Error Resume Next
s0 = ""
'#Old: For xx = 0 To x
'Update: convert x to int
For xx = 0 To Convert.ToInt32(x)
'#UpdateEnd
If fname.ch(xx) = 0 Then xx = 99999 Else s0 = s0 & Chr(fname.ch(xx))
Next xx

System.Diagnostics.Debug.WriteLine(sVbZipInf & s0)
msOutput = msOutput & s0

sVbZipInf = ""

System.Windows.Forms.Application.DoEvents()
DLLPrnt = 0

End Function



It's the first time i use delegates, it's also the first time i use vb.net
so i have absolutly no idea what to do here. My delegate-try is completely wrong, isn't it :o
 
the delegate definition needs to match with the function definition and vice-versa so in your case the delegate needs to be
Visual Basic:
Delegate Function DLLPrntDelegate(ByRef fname As CBChar, ByVal x As Integer)

Is the dll also VB6 / vb.net or is it written in another language?
If in .Net you shouldn't require things like the FnPtr function to generate an integer pointer to the function.

In all honesty the migration wizard is pretty useless and tends to generate code that doesn't actually compile and even if it does it rarely works correctly.
If this is your first foray into VB.Net you will find the wizard generated code more confusing and useless than of any real benefit. You are probaby better of re-writing the code yourself.
 
Sorry, but when i make the delegate as you tell, there's the same error:
AddressOf expression cannot be converted...

in the line
MYUSER.DLLPrnt = FnPtr(AddressOf DLLPrnt)

"AddressOf DLLPrnt" is underlined

so removing FnPtr isn't possible because the error is in 'AddressOf'
 
Last edited:
If you are using delegates then you shouldn't need the FnPtr call. However this depends on how the DLL is constructed - what language was it written in and do you have any control over it's code?`
 
PlausiblyDamp said:
If you are using delegates then you shouldn't need the FnPtr call. However this depends on how the DLL is constructed - what language was it written in and do you have any control over it's code?`


I didn't write the vb6 version of this program, i only got the source to upgrade it.

There are 2 .dll files: zip32.dll and Unzip32.dll

i don't know what language they're written.
 
I think it works now.

I added the delegate like you told me:
Delegate Function DLLPrntDelegate(ByRef fname As CBChar, ByVal x As Integer)

then i changed the type of MYUSER.DLLPrnt from integer to DLLPrntDelegate
and the line
MYUSER.DLLPrnt = FnPtr(AddressOf DLLPrnt)
i changed to:
MYUSER.DLLPrnt = AddressOf DLLPrnt

was this you tried me to tell

now i get no Build Error for this line

could it be that it works now?
I can't test it, because there are still some other build errors...


thanks for your link, if it still doesn't work i'll check your link
 
I have a similar problem. I read your answers but ı couldnt solve that. My English and my visual basic knowledge isn't enough ı think.

FPrevWinProc = SetWindowLong(FWinHandle, GWL_WNDPROC, AddressOf WinProc)

this line returns;

'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type.

here is the declaration of function.

Private Function WinProc(ByVal pHWnd As Integer, ByVal pMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

what I must do. How can I solve this problem. Can anyone tell this for me with clear sentences. Thanks for your assistance.
 
first you need to make a delegate function ( YOU MUST NAME IT DIFFERENTLY TO YOUR ACTUAL FUNCTION )
ie:
Visual Basic:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Delegate [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] WProc([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] pHWnd [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] pMsg [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] wParam [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] lParam [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2]) [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size]
[size=2][color=#0000ff]
[/color]then modify the SetWindowLong API so the last parameter is WProc as above
ie:
Visual Basic:
[size=2][/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Declare [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] SetWindowLong [/size][size=2][color=#0000ff]Lib[/color][/size][size=2] "user32.dll" [/size][size=2][color=#0000ff]Alias[/color][/size][size=2] "SetWindowLongA" ([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] hwnd [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] nIndex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] dwNewLong [/size][size=2][color=#0000ff]As[/color][/size][size=2] WProc) [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32[/size]
[size=2]
[/size]
then your code will work, here's a quick example ...
Visual Basic:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] GWL_WNDPROC [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32 = -4[/size]
 
[size=2]
[/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Declare [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] SetWindowLong [/size][size=2][color=#0000ff]Lib[/color][/size][size=2] "user32.dll" [/size][size=2][color=#0000ff]Alias [/color][/size][size=2]"SetWindowLongA" ([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] hwnd [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] nIndex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] dwNewLong [/size][size=2][color=#0000ff]As[/color][/size][size=2] WProc) [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32[/size]
 
[size=2]
[/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Delegate [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] WProc([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] pHWnd [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] pMsg [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] wParam [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] lParam [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2]) [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size]
 
[size=2][color=#0000ff]
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] FPrevWinProc [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size]
 
 
[size=2][color=#0000ff]
[/color][/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] WinProc([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] pHWnd [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] pMsg [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] wParam [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] lParam [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2]) [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size]
[size=2][color=#0000ff]
[/color][/size]
[size=2][color=#008000]
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Function[/color][/size]

 
[size=2][color=#0000ff]
[/color][/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Button1_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] Button1.Click[/size]
 
[size=2]
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] FWinHandle [/size][size=2][color=#0000ff]As[/color][/size][size=2] Int32 = [/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Handle.ToInt32[/size]
[size=2]
FPrevWinProc = SetWindowLong(FWinHandle, GWL_WNDPROC, [/size][size=2][color=#0000ff]AddressOf[/color][/size][size=2] WinProc)[/size]
 
[size=2]
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]
hope that explains things alittle

[/size]
 
Back
Top