VB.NET Nightmare
4 отговора
268 прегледа
VB.NET Nightmare
Искам да направя нещо доста елементарно на ВБ, резултатът обаче за сега е куп строшени нерви. Имам една DLL функция, която извиква фунция на ВБ. ВБ приложението би трябвало да задели един буфер от няколко байта и да върне указател към него, така че DLL-а да може да го ползва после. Само че това нещо с "адреса на буфера" засега ми се струва невъзможно. Единсвеното на което попаднах е VarPtr и ObjPtr, които обаче в .NET са изхвърлени. Така че за момента нямам идея - моля някой ако е наясно да помогне. Отдавам проблема на малкия си опит с ВБ, просто не мога да си представя че ползван от толкова много хора език няма такава елементарна възможност. Иначе по ред причини е неприложимо да се променя интерфейса.
Re: VB.NET Nightmare
Ами пробвай да натъпчеш нещото във variant и да го предадеш указател към него
Re: VB.NET Nightmare
Не зная как точно са организирани нещата в VB.NET, но в Delphi има възможност за използване на UNSAFE code. Друг вариант е да се използва IntPtr (това е май единствената pointer structure, която съществува в .NET).
Погледни тук [линк]. Особено тази част :
VarPtr, StrPtr, ObjPtr
These undocumented functions have traditionally been used by Visual Basic experts to find the memory addresses where variables or objects were stored. These functions allowed addresses to be passed to a DLL routine or to the Windows API, which sometimes required this information. In Visual Basic .NET, you hopefully won't need to have this kind of low-level access to memory information, as it complicates programs and can introduce obscure bugs. If, however, you need to interact with a DLL or code component that needs an address, you can still retrieve it-but you need to "pin down" the memory first. Pinning down the memory ensures that the Common Language Runtime won't try to move a value between the time when you find its address and the time when the DLL tries to use it. (This automatic movement feature is one of the ways by which the .NET runtime attempts to improve performance.)
The following example creates and pins down a handle for an object called MyObj. It uses a special type called GCHandle in the System.Runtime. InteropServices namespace:
' You will need to import the namespace shown below to use this code as written.
' Imports System.Runtime.InteropServices
Dim MyGCHandle As GCHandle = GCHandle.Alloc(MyObj, GCHandleType.Pinned)
Dim Address As IntPtr = MyGCHandle.AddrOfPinnedObject()
' (Invoke the DLL or do something with the Address variable here.)
' Allow the object to be moved again.
MyGCHandle.Free()
Of course, the coding you use is up to you, but be aware that the Upgrade Wizard will simply place an error flag if it finds the unsupported VarPtr, StrPtr, or ObjPtr function.
Погледни тук [линк]. Особено тази част :
VarPtr, StrPtr, ObjPtr
These undocumented functions have traditionally been used by Visual Basic experts to find the memory addresses where variables or objects were stored. These functions allowed addresses to be passed to a DLL routine or to the Windows API, which sometimes required this information. In Visual Basic .NET, you hopefully won't need to have this kind of low-level access to memory information, as it complicates programs and can introduce obscure bugs. If, however, you need to interact with a DLL or code component that needs an address, you can still retrieve it-but you need to "pin down" the memory first. Pinning down the memory ensures that the Common Language Runtime won't try to move a value between the time when you find its address and the time when the DLL tries to use it. (This automatic movement feature is one of the ways by which the .NET runtime attempts to improve performance.)
The following example creates and pins down a handle for an object called MyObj. It uses a special type called GCHandle in the System.Runtime. InteropServices namespace:
' You will need to import the namespace shown below to use this code as written.
' Imports System.Runtime.InteropServices
Dim MyGCHandle As GCHandle = GCHandle.Alloc(MyObj, GCHandleType.Pinned)
Dim Address As IntPtr = MyGCHandle.AddrOfPinnedObject()
' (Invoke the DLL or do something with the Address variable here.)
' Allow the object to be moved again.
MyGCHandle.Free()
Of course, the coding you use is up to you, but be aware that the Upgrade Wizard will simply place an error flag if it finds the unsupported VarPtr, StrPtr, or ObjPtr function.
Re: VB.NET Nightmare
В .Net всякакви указатели, дръжки (handles) и подобни са заменени с IntPtr. BSTR става най-обикновен стринг, както и всички вариации на LPSTR. Доколкото схващам, ти се опитваш да използваш най-обикновен Win32 dll от .Net приложение. Нали се сещаш, че за тази цел трябва първо да си декларираш функциите от тоя dll с DllImport, като си направиш съответния "превод" на типовете. И разгледай внимателно System.Runtime.InteropServices неймспейса.
но след безсънни дълги нощи станах силен и голям
и вече знам, че мога да се справя сам
но след безсънни дълги нощи станах силен и голям
и вече знам, че мога да се справя сам