Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. kernel32.dll Call Problems

kernel32.dll Call Problems

Scheduled Pinned Locked Moved Visual Basic
csharpjsonhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pugman812
    wrote on last edited by
    #1

    I am tring to use the copymemory API and it works fine in vb 6.0 but when i try to use this in .net. It throws an error and says that the object is not set to an instance. The API is declared in a public module and you do not have to instance a module. Well i never have and i have also tried putting the call in the class im working in. Still nothing. I have tried many variations in the coding of the call to satisfy its needs but always throws the error. This is the code that i am using in .NET Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Int32, ByRef Source As Object, ByVal Length As Int32) This is the code that works in 6.0 Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any,ByVal Length As Long) And also is the a equivalent of the any data type in 6.0 Object in a manner of speaking.

    D 1 Reply Last reply
    0
    • P Pugman812

      I am tring to use the copymemory API and it works fine in vb 6.0 but when i try to use this in .net. It throws an error and says that the object is not set to an instance. The API is declared in a public module and you do not have to instance a module. Well i never have and i have also tried putting the call in the class im working in. Still nothing. I have tried many variations in the coding of the call to satisfy its needs but always throws the error. This is the code that i am using in .NET Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Int32, ByRef Source As Object, ByVal Length As Int32) This is the code that works in 6.0 Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any,ByVal Length As Long) And also is the a equivalent of the any data type in 6.0 Object in a manner of speaking.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Yout want to post the code that is throwing the exception, including the above declare. What your doing will not directly translate to VB.NET simply because your trying to marshal managed objects to unmanaged code. We need the code, including what your trying to copy, and what your trying to accomplish in order to help you. There may be alternative ways to do this... RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome

      P 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Yout want to post the code that is throwing the exception, including the above declare. What your doing will not directly translate to VB.NET simply because your trying to marshal managed objects to unmanaged code. We need the code, including what your trying to copy, and what your trying to accomplish in order to help you. There may be alternative ways to do this... RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome

        P Offline
        P Offline
        Pugman812
        wrote on last edited by
        #3

        This is a blowfish cryptography program that i am converting from 6.0 to .NET. This project has to 2 Textboxs "Textbox1, Textbox2" 2 buttons "btnEncrypt, btnDecrypt" and 1 active X control named VbCryptoEngine1 This program begins when you click the encrypt button Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click VbCryptoEngine1.CryptAlgorithm = CryptoEngine.vbCryptoEngine.enuCRYPTO_ALGORITHMS.acuBlowfish TextBox2.Text = VbCryptoEngine1.EncryptString(TextBox1.Text, VbCryptoEngine1.key, True) End Sub The VbCryptoEngine1.CryptAlgorithm line works fine it sets a new instance of the class blowfish where all the code for blowfish is contained. the function EncryptString looks like this Public Function EncryptString(ByVal Text As String, Optional ByVal Key As String = "Cool", _ Optional ByVal OutputInHex As Boolean = False) As String On Error GoTo errhandler If m_status = STAT_BUSY Or m_status = STAT_ERROR Then Exit Function EncryptString = m_Engine.EncryptString(Text, Key, OutputInHex) m_status = STAT_READY RaiseEvent statuschanged(m_status) Exit Function errhandler: ' --------------------------------------------------------------------------- ' Raise friendly error to the handler ' --------------------------------------------------------------------------- Call Err.Raise(Err.Number, "CryptoEngine:EncryptString", Err.Description) End Function I was going to us the try catch statement but i will update code later. the line with the blue sends the string info and a few other variables to the instances blowfish class The Function looks like this Public Function EncryptString(ByVal Text As String, Optional ByVal Key As String = "Cool", Optional ByVal OutputInHex As Boolean = False) As String Implements IAlgorithm.EncryptString Dim ByteArray() As Byte Dim [unicode] As Encoding = Encoding.Unicode Dim ascii As Encoding = Encoding.ASCII Dim unicodebytes As Byte() Dim asciibytes As Byte() m_status = STAT_BUSY RaiseEvent statuschanged(m_status) On Error GoTo errhandler unicodebytes = [unicode].GetBytes(Text) Encoding.Convert([unicode], Encoding.Default, unicodebytes) ByteArray = unicodebytes

        D 1 Reply Last reply
        0
        • P Pugman812

          This is a blowfish cryptography program that i am converting from 6.0 to .NET. This project has to 2 Textboxs "Textbox1, Textbox2" 2 buttons "btnEncrypt, btnDecrypt" and 1 active X control named VbCryptoEngine1 This program begins when you click the encrypt button Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click VbCryptoEngine1.CryptAlgorithm = CryptoEngine.vbCryptoEngine.enuCRYPTO_ALGORITHMS.acuBlowfish TextBox2.Text = VbCryptoEngine1.EncryptString(TextBox1.Text, VbCryptoEngine1.key, True) End Sub The VbCryptoEngine1.CryptAlgorithm line works fine it sets a new instance of the class blowfish where all the code for blowfish is contained. the function EncryptString looks like this Public Function EncryptString(ByVal Text As String, Optional ByVal Key As String = "Cool", _ Optional ByVal OutputInHex As Boolean = False) As String On Error GoTo errhandler If m_status = STAT_BUSY Or m_status = STAT_ERROR Then Exit Function EncryptString = m_Engine.EncryptString(Text, Key, OutputInHex) m_status = STAT_READY RaiseEvent statuschanged(m_status) Exit Function errhandler: ' --------------------------------------------------------------------------- ' Raise friendly error to the handler ' --------------------------------------------------------------------------- Call Err.Raise(Err.Number, "CryptoEngine:EncryptString", Err.Description) End Function I was going to us the try catch statement but i will update code later. the line with the blue sends the string info and a few other variables to the instances blowfish class The Function looks like this Public Function EncryptString(ByVal Text As String, Optional ByVal Key As String = "Cool", Optional ByVal OutputInHex As Boolean = False) As String Implements IAlgorithm.EncryptString Dim ByteArray() As Byte Dim [unicode] As Encoding = Encoding.Unicode Dim ascii As Encoding = Encoding.ASCII Dim unicodebytes As Byte() Dim asciibytes As Byte() m_status = STAT_BUSY RaiseEvent statuschanged(m_status) On Error GoTo errhandler unicodebytes = [unicode].GetBytes(Text) Encoding.Convert([unicode], Encoding.Default, unicodebytes) ByteArray = unicodebytes

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          First, why are you even using CopyMemory? From what I see here you don't need it. OK. You've got two problems. Your code:

          Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Int32, ByRef Source As Object, ByVal Length As Int32)
          .
          .
          .
          call CopyMemory(CInt(ByteArray(12)), CInt(ByteArray(0)), CInt(OrigLen))
          .
          .
          .

          What your doing here is nasty to say the least. CopyMemory takes 3 parameters, a Destination address, a Source address, and a Length to copy, in bytes. What you've done is tell VB to marshal the Destination address as an Int32 and the Source as a reference to an Object. What your doing is converting ByteArray(12) and ByteArray(0) to Integers, then by the definition you setup in the Declare statement, you're passing the VALUE of ByteArray(12) to CopyMemory, NOT the address! Next, your passing a reference (or address) as the second argument, but your passing an Object reference to another byte converted to an Integer. CopyMemory expects an address for the first and second arguments, you got one right. You might want to take a look at this[^] article before trying to use CopyMemory. RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome

          P 1 Reply Last reply
          0
          • D Dave Kreskowiak

            First, why are you even using CopyMemory? From what I see here you don't need it. OK. You've got two problems. Your code:

            Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Int32, ByRef Source As Object, ByVal Length As Int32)
            .
            .
            .
            call CopyMemory(CInt(ByteArray(12)), CInt(ByteArray(0)), CInt(OrigLen))
            .
            .
            .

            What your doing here is nasty to say the least. CopyMemory takes 3 parameters, a Destination address, a Source address, and a Length to copy, in bytes. What you've done is tell VB to marshal the Destination address as an Int32 and the Source as a reference to an Object. What your doing is converting ByteArray(12) and ByteArray(0) to Integers, then by the definition you setup in the Declare statement, you're passing the VALUE of ByteArray(12) to CopyMemory, NOT the address! Next, your passing a reference (or address) as the second argument, but your passing an Object reference to another byte converted to an Integer. CopyMemory expects an address for the first and second arguments, you got one right. You might want to take a look at this[^] article before trying to use CopyMemory. RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome

            P Offline
            P Offline
            Pugman812
            wrote on last edited by
            #5

            What suggestions do you have for the replacements for my current code.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups