🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.vbasic
Article #92493

DLL - Go To Hell

#92493
From: Benek
Date: Mon, 31 Oct 2005 15:28
145 lines
7357 bytes
Poprzednio (w poprzednim poscie dot. tego problemu) postawilem zla diagnoze, 
mianowicie myslaem ze wywolywanie formatek spod bibliotek wywoluje blad. 
Jednak znalazlem inna przyczyne: funkcja API SetWindowLong albo CallWindowProc 
nie dziala prawidlowo pod platforma .NET. identyczny kod(po paru malych 
poprawkach zwiazanych ze zgodnoscia) dziala w 100% prawidlowo pod VB6. Na 
pierwszy rzut oka wszystko jest w pozadku, ale po kolei wytlumacze zeby bylo 
wszystko wiadomo.
1. Wczytuje Biblioteke za pomoca LoadForm
2. Tworze item w menu o nazwie "Glowna" (nalezy uprzednio dodac komponent 
MainMenu do formatki"
3. Otwieram Instancje do Procedury ktora tworzy nastepny Item "Item1"

No i do tego momentu wszystko dzaiala, nastepne punkty tez wykonuja sie bez 
blednie....

4. Wywoluje funkcje SetWindowLong (jak rozumiem jest to funkcja ktora moze 
zmieniac uchwyty do roznych rzeczy :) )
5. Wszystko sie wczytuje OK.
6. Wybieram z menu glowne, Item1. Wszystko gra - wywolywuje sie formatka, 
ale... W momencie kiedy lapie za belke formatki i ruszam (szubko) ta formatka 
po obrebie (albo po nim) calego okna z ktorego zostala wywolana w tym 
przypadku Form1 to wywoluje sie nieznany blad: 
|------------------------------------------------------------------------------
|An unhandled exception of type 'System.NullReferenceException' occurred in 
|Unknown Module.			                                         
|Additional information: Object reference not set to an instance of an object 
|------------------------------------------------------------------------------
albo
|------------------------------------------------------------------------------
|An unhandled exception of type 'System.Runtime.InteropServices.SEHException' 
|occurred in Unknown Module.					 
|Additional information: External component has thrown an exception.	
|------------------------------------------------------------------------------
albo
|-----------------------------------------------------------------------------
|An unhandled exception of type 'System.Runtime.InteropServices.SEHException' 
|occurred in system.windows.forms.dll|
|Additional information: External component has thrown an exception.
|-----------------------------------------------------------------------------
tak mniej wiecej po 10s bardzo szybkiego ruszania ta formatka powinien ten 
blad sie pojawic.

Moje podejrzenia:
Prawdopodobnie funkcja CallWindowProc nie dziala prawidlowo, bo nie zwraca 
funkcji SetWindowLong prawidlowego starwgo uchwytu do komponentu ktory 
wywoluje funkcja SetWindowLong. Wiem ze jest w .NET zamiennik CallWindowLong : 
System.Windows.Forms.Form.WndProc, ale nie wiem jak z niej korzystac. moze 
dlatego dali zamiennik tej funkcji bo byla zla? Nie wiem tego. W kazdym razie 
nie dziala tak samo jak w VB6. W zwiazku z problemem AddressOf musialem dodac 
Dodac Delegate do funkcji SetWindowLong. Najchetniej nie korzystalbym z API 
ale nie wiem jak za pomoca "czystego" VB2003 cos takiego zrobic. Juz siedze 
tydzien nad tym ale nie przychodzi mi nic do glowy, moze Ktoś sie znajdzie kto 
by mogl mi pomoc. Wiem ze sprawa nie jest latwa.  


KOD W FORMATCE:

Public Class Form1
Inherits System.Windows.Forms.Form

    Delegate Function MenuHandlerDelegate(ByVal hwnd As Integer, ByVal Msg As 
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal 
hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As 
MenuHandlerDelegate) As Integer
    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" 
(ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal Msg As Integer, 
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hmenu 
As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer, ByVal 
wIDNewItem As Integer, ByVal lpNewItem As String) As Integer
    Declare Function GetMenu Lib "user32" (ByVal hwnd As Integer) As Integer
    Declare Function CreatePopupMenu Lib "user32" () As Integer
    Const GWL_WNDPROC = (-4)
    Const WM_COMMAND = &H111
    Const MF_BYPOSITION& = &H400&
    Const MF_POPUP& = &H10&

    Public asm As System.Reflection.Assembly
    Public hOldProc As Integer
'------------------------------------------------------------------------------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
        Dim o As Object
        Dim hmen As Integer, hsub As Integer

        asm = Reflection.Assembly.LoadFrom("C:\ClassLibrary1.dll")
        hmen = GetMenu(Me.Handle.ToInt32)
        hsub = CreatePopupMenu()
        InsertMenu(hmen, 3, MF_BYPOSITION Or MF_POPUP, hsub, "Glowne")

        o = asm.CreateInstance("ClassLibrary1.Class1")
        o.MENU(hsub)

        hOldProc = SetWindowLong(Me.Handle.ToInt32, GWL_WNDPROC, AddressOf 
MenuHandler)
    End Sub
'------------------------------------------------------------------------------
    Public Function MenuHandler(ByVal hwnd As Integer, ByVal Msg As Integer, 
ByVal wParam As Integer, ByVal lParam As Integer) As Integer

        Dim i As Integer
        Dim f As Form

        Select Case Msg
            Case WM_COMMAND
                Select Case wParam
                    Case 300
                        f = CType(asm.CreateInstance("ClassLibrary1.Form11"), 
Form)
                        f.MdiParent = Me
			f.Show()
                    Case Else
                        MenuHandler = CallWindowProc(hOldProc, hwnd, Msg, 
wParam, lParam)
                End Select
            Case Else
                MenuHandler = CallWindowProc(hOldProc, hwnd, Msg, wParam, 
lParam)
        End Select

    End Function
'------------------------------------------------------------------------------
End Class




KOD W BIBLIOTECE ClassLibrary1:
Biblioteka zawiera jeszcze formatke o nazwie: "Form11", ktora ma zostac 
wywolana.

Public Class Class1
    Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hmenu 
As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal 
lpNewItem As String) As integer
    Const MF_STRING& = &H0&
'------------------------------------------------------------------------------
    Public Sub MENU(ByVal hsub As Integer)
        AppendMenu(hsub, MF_STRING, 300, "Item1")
    End Sub
End Class

-- 
Wysłano z serwisu OnetNiusy: http://niusy.onet.pl
Technical Details
Message-ID: <1281.000009a8.43663828@newsgate.onet.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.onet.pl!newsgate.onet.pl!niusy.onet.pl

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.onet.pl!newsgate.onet.pl!niusy.onet.pl
From: "Benek" <overkill@op.pl>
Newsgroups: pl.comp.lang.vbasic
Subject: DLL - Go To Hell
Date: 31 Oct 2005 16:28:41 +0100
Organization: Onet.pl SA
Lines: 145
Message-ID: <1281.000009a8.43663828@newsgate.onet.pl>
NNTP-Posting-Host: newsgate.onet.pl
Mime-Version: 1.0
X-Trace: newsgate.test.onet.pl 1130772521 11992 213.180.130.18 (31 Oct 2005 15:28:41 GMT)
X-Complaints-To: abuse@onet.pl
NNTP-Posting-Date: 31 Oct 2005 15:28:41 GMT
X-Mailer: http://niusy.onet.pl
X-Forwarded-For: 217.113.239.172, 192.168.243.35
X-User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit