CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    [RESOLVED] Copymem API Error - wierd one ????

    Now this is a very wierd problem...
    Code:
    Private Block As String
    Private File_Block As Long
    Private Sub MoveData ()
    Dim arr_dat() As Byte
    Get_Data () ' Function that returns 1024 bytes in Arr_dat..
    	File_block = 1024
    	Block = Space(1024)
    	CopyMemory Block, arr_dat(0), File_Block ' this now shuts down the IDE...
    End Sub
    the CopyMemory API causes the ide to exit ...

    I'm trying to move and array of binary data into a string without resorting to a loop... And this one has me going mad...

    Any Advice ???

    Gremmy....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  2. #2
    Join Date
    Mar 2006
    Posts
    135

    Re: Copymem API Error - wierd one ????

    Use this:
    StringVariable = StrConv(ByteArray, vbUnicode)

  3. #3
    Join Date
    Aug 2006
    Posts
    145

    Re: Copymem API Error - wierd one ????

    well, each character of a string is two bytes, not one - so the string has to be half the size of the byte array. You also need to pass the memory location of the String's byte array, rather than the BSTR:
    Code:
        Block = Space(1024 \ 2)
        CopyMemory ByVal StrPtr(Block), arr_dat(0), File_Block
    that now won't crash your IDE, but it may not be what you're intending to achieve. You may want what logophobic posted, or perhaps even a simple:
    Code:
    Block = arr_dat
    might achieve your needs...

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Copymem API Error - wierd one ????

    or you may just specify byval to the string variable (Block).

    CopyMemory ByVal Block, arr_dat(0), File_Block
    Busy

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Copymem API Error - wierd one ????

    Quote Originally Posted by Logophobic
    Use this:
    StringVariable = StrConv(ByteArray, vbUnicode)
    Thanks LogoPhobic...

    This works great .. Duno why i didn't remember the function....

    Reps when i can give again...

    Gremmy
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Copymem API Error - wierd one ????

    hi Gremmy,

    perhaps we have different declaration of the Copymemory. the following code works well on my computer

    Code:
    Option Explicit
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    
    
    Private Sub Command1_Click()
    Dim s$, b() As Byte
      
      b = StrConv("one last breath", vbFromUnicode)
      
      s = Space$(UBound(b) + 1)
      CopyMemory ByVal s, b(0), Len(s)
      
      MsgBox s
      
    End Sub
    Busy

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Copymem API Error - wierd one ????

    Thread1..

    I have it declared the same way..
    Code:
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    And for some reason it closes the IDE down - No error msg, no nothing...

    Dunno why.. at a later stage i will look at it again and try it again..

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  8. #8
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Copymem API Error - wierd one ????

    ok.. let me say the word again "weird" .. yeah that's weird
    Busy

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

Featured