Results 1 to 12 of 12

Thread: How to build SafeArray in VB.Net ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    236

    How to build SafeArray in VB.Net ?

    Hi , all

    How to build SafeArray ( int type ) in VB.Net and use the SafeArray in VB.Net ?

    thank you very much
    Last edited by quickbbbb; Jun 17th, 2018 at 09:44 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to build SafeArray in VB.Net ?

    From what I can see, a SafeArray has no place. The functionality is just an array of integer in VB. The reasons I saw for creating a SafeArray don't seem to serve any purpose because the functionality is already contained within an array.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to build SafeArray in VB.Net ?

    Please provide a FULL and CLEAR explanation of the problem. There is no such type as SafeArray in the .NET Framework. Are you talking about this?

    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    If so, what is it for? Are you trying to call an API function? If so, which one?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    236

    Re: How to build SafeArray in VB.Net ?

    Quote Originally Posted by Shaggy Hiker View Post
    don't seem to serve any purpose
    SafeArray can be
    (1) Lbound <> 0
    (2) Bind your Unmanaged memory pointer
    Last edited by quickbbbb; Jun 21st, 2018 at 08:59 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    236

    Re: How to build SafeArray in VB.Net ?

    Quote Originally Posted by jmcilhinney View Post
    Please provide a FULL and CLEAR explanation of the problem. There is no such type as SafeArray in the .NET Framework.
    Name:  004456.jpg
Views: 762
Size:  9.3 KB

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to build SafeArray in VB.Net ?

    Quote Originally Posted by quickbbbb View Post
    Name:  004456.jpg
Views: 762
Size:  9.3 KB
    I'm not sure that you understand what the words "full" and "clear" mean. A screenshot doesn't qualify as a full and clear description. Not only that, what the screenshot displays is the SafeArray field of the UnmanagedType enumeration, so it doesn't do anything to refute the statement that there is no such type as SafeArray in the .NET Framework. The documentation for that UnmanagedType enumeration even says:
    This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
    It also says:
    Identifies how to marshal parameters or fields to unmanaged code.
    If you want to use it, that seems to imply that you have some data that you want to pass to unmanaged code, so I go back to my previous questions:
    what is it for? Are you trying to call an API function? If so, which one?
    The idea with that enumeration is you specify what unmanaged type should be used to represent your managed type when an instance is passed to unmanaged code. If you're not willing to provide any context here then it makes it hard for us to know what you should do to achieve your aim. Invoking unmanaged code from managed code is a bit of a black art so we really need specific information in order to provide advice.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    236

    Re: How to build SafeArray in VB.Net ?

    sorry , my english is poor , can not has full expression

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to build SafeArray in VB.Net ?

    I understand that it can be difficult if English is not your first language but we can't read your mind. If you can't at least make an attempt to provide an explanation then you're probably not going to get an answer that helps.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to build SafeArray in VB.Net ?

    How about typing into Google Translate in whichever language you feel more comfortable with. That might result in garbage, but those translators are getting better all the time.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    236

    Re: How to build SafeArray in VB.Net ?

    VB6 Array is SafeArray

    I can tampered its memory pointer

    example:

    ' VB6 Code

    Redim AR_001(1 to 1000) as long
    Redim AR_002(1 to 1000) as long

    Debug.Print Varptr( AR_001(1) ) ' ----> Debug Windows show 1731096
    Debug.Print Varptr( AR_002(1) ) ' ----> Debug Windows show 1731576

    Call T_Bind_Pointer_To_Array ( AR_002 , Varptr( AR_001(1) ) ) '

    ' after call T_Bind_Pointer_To_Array , Varptr(AR_002) will be equal Varptr(AR_001)

    Debug.Print Varptr( AR_001(1) ) ' ----> Debug Windows show 1731096
    Debug.Print Varptr( AR_002(1) ) ' ----> Debug Windows show 1731096

    ========================

    whether VB.Net Array Pointer can be tampered by user ?

    if VB.Net can not , I Had no choice but to use SafeArray in VB.NET

    because SafeArray can be tampered its memory pointer

    but how to build SafeArray in VB.NET ?? And I use the SafeArray can be as same as VB.Net Array

    ex: in VB.Net

    Dim AR_001( ) As SafeArray

    Call VB_Net_Build_SafeArray( AR_001 , Long_Type , lb:=0 , ub:=100 )

    Dim AR_002() As Long ' VB.Net Array
    ReDim AR_002(1 to 100)

    For w= 1 to 100
    AR_001(w) = w ' -> use way as same as VB.Net Array
    AR_002(w) = w
    next

    ========================

    if VB.Net Array Pointer can be tampered by user , That is the best

    beacuse I need not use safearray
    Last edited by quickbbbb; Jun 20th, 2018 at 11:02 PM.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to build SafeArray in VB.Net ?

    All arrays in .NET have a lower bound of 0. That could be changed in VB6, but not in .NET. Of course, the lower bound is mostly made up anyways. As you know, an array is just a sequence of bytes in memory, so whether you call the first element in that sequence 0, 1, 100, or Fred, really doesn't matter.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    236

    Re: How to build SafeArray in VB.Net ?

    Quote Originally Posted by Shaggy Hiker View Post
    really doesn't matter.
    It concerning the ease of use and not easy to make mistakes

    // VB6

    Redim AR_001(-100 To 100) As long

    ID = -100 ' ID Range = -100 ~ 100

    AR_001(ID ) = AR_001(ID ) + Rnd

    Debug.Print AR_001(ID )


    // VB.Net

    Redim AR_001(0 To 201) As long

    ID = -100 ' ID Range = -100 ~ 100

    const AR_Shift = 100

    AR_001(ID +AR_Shift) = AR_001(ID +AR_Shift) + Rnd

    Debug.Print AR_001(ID +AR_Shift ) ---> user always need add a shift index and add a const value to remember shift index is 100

    if AR_001 used in very many module or sub , There is always a chance to forget to add a shift index

    ex:

    ID = 2
    you use AR_001( ID ) --> error ; because you has not add shift index

    and ID=2 is inside of AR_001 range , VB.Net can not tell you it is error
    Last edited by quickbbbb; Jun 21st, 2018 at 10:28 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width