vb.net - Calling a Class vs Structure error thrown -


can explain why works:

 <structlayout(layoutkind.sequential, charset:=charset.auto)> _     public class memorystatusex           public sub new()             me.dwlength = ctype(marshal.sizeof(gettype(memorystatusex)), uint32)         end sub          public dwlength uint32          public dwmemoryload uint32          public ulltotalphys uint64          public ullavailphys uint64          public ulltotalpagefile uint64          public ullavailpagefile uint64          public ulltotalvirtual uint64          public ullavailvirtual uint64          public ullavailextendedvirtual uint64     end class 

and doesn't:

 <structlayout(layoutkind.sequential, charset:=charset.auto)> _     structure memorystatusex          public sub new(byval dwlength uint32)             me.dwlength = dwlength         end sub          public dwlength uint32          public dwmemoryload uint32          public ulltotalphys uint64          public ullavailphys uint64          public ullavailpagefile uint64          public ulltotalvirtual uint64          public ullavailvirtual uint64          public ullavailextendedvirtual uint64     end structure 

when calling both structures/class class this:

 dim newpoint new structures.memorystatusex()          globalmemorystatusex(newpoint)  

and 2nd:

 dim newpoint new structures.memorystatusex(ctype(marshal.sizeof(gettype(structures.memorystatusex)), uint32))              globalmemorystatusex(newpoint)  

they're both inside class, when call second size parameter throws "a first chance of accessviolationexception" on globalmemorystatusex(newpoint) call , crashes application.

i can't understand why since dwlength value initialized on both in constructor? right?

the reason want change first example because i'm moving structures structure-only class, , thought idea until coudln't understand why didn't work, since same value set before call api occurs.

p/invoke declarations:

    <dllimport("kernel32.dll", setlasterror:=true)> _     private shared function globalmemorystatusex(lpbuffer structures.memorystatusex) <marshalas(unmanagedtype.bool)> boolean     end function 

error's detail:

a first chance exception of type 'system.accessviolationexception' occurred in client.exe

additional information: attempt of read or write protected memory...

if click continue:

a first chance exception of type 'system.reflection.targetinvocationexception' occurred in mscorlib.dll

additional information: exception has been thrown target of invocation

  private shared function globalmemorystatusex(lpbuffer structures.memorystatusex) ... 

the lpbuffer argument pointer. required function can write structure members. pointers well-hidden in vb.net, blow programs smithereens when not used properly. lots of possible mishaps, accessviolationexception milder variety.

two basic ways pointer in vb.net. can declare argument byref if argument value type. or if reference type (class keyword) reference automatically becomes pointer @ runtime , should passed byval.

so valid combinations structure passed byref or class passed byval. class requires explicit <structlayout> force clr order fields in predictable manner. free structure. therefore logical choice.

you should using my.computer.info, pinvokes globalmemorystatusex() you.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -