Declairing API's - As Any Overloads

Denaes

Well-known member
Joined
Jun 10, 2003
Messages
956
Heres an example:

Code:
 Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
      ByRef Destination As Any, _
      ByRef Source As Any, _
      ByVal Length As Int32)

This is the declairation, but there arnt any "Any" in .Net, Object is closer, but it would be better to overload the function/sub to handle certain conditions.

I keep trying different ways of overloading, but I get errors about expecting the end of the procedure which isnt there because its an API declaration.
 
Well, that looks interesting

While that link does have some interesting ideas and sure is useful, the issue isnt CopyMemory.

There are more than a few windows APIs that have a declaration "As Any". "As Any" was basically a varient to leave a variable open to anything from string, pictureboxes, integers and buffers.

In .Net, you can overload a function so that you have many different functions with the same name, but different parameters. If youre given a picturebox as a parameter, it looks for the CopyMemory using Picturebox as a parameter - if non is found an exception is thrown.

Im sure you can use "As Object" as everything in .Net is an object, but thats supposed to be poor programming and in this particular case, copymemory is supposed to quickly copy units of memory, so speed should be of the essence.

I can "Jig" it right now only using one form of the function (from a back buffer to a picturebox), but Id like to learn the proper way to overload an API call without it giving an error and expecting a "End Function" or "End Sub" at the end.

That link you gave me does look interesting, though at the moment it just overloaded my head and sent me running for caffine. Ill have to settle down with that and figure it out :P
 
Maybe this is what youre looking for...
Code:
<Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint:="RtlMoveMemory")> Public Shared Function CopyMemory(ByVal dest As String, ByVal source As String, ByVal length As Integer) As Integer
 Keep this empty
End Function

The DllImportAttribute is another way to call Windows APIs in
.NET apps, just make sure the method youre attributing is static
(Shared in VB.NET). Anyway, using this method (no pun intended)
you can then created overloaded versions of it with different
parameters.
 
yeah, Im pretty sure thats what Im looking for... though the first part is something Im not used to seeing:

"<Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint:="RtlMoveMemory")>"

Not used to brackets in my VB... I guess its universal .Net stuff :P

It looks like it works... And I can declare it over and over again with different arguements? Or do I still need to declair them all as Overload functions?
 
You can declare them as Overloads if you want, but the compiler
will figure it out itself if you dont. So yes, you can change the
parameters.

The <>s (which are []s in C#) represent an Attribute. Attributes
are special... attributes (for lack of a better word) which you can
add to the declaration of any class, method, etc. (assuming that
attribute supports it; you wouldnt obviously put a DllImportAttribute,
which imports a method from a DLL, on a class). [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/vbcn7/html/vaconAttributesOverview.htm]Read more here[/mshelp].
 

Similar threads

Back
Top