Thanks!!! Re: call c/c++ function from VB


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Thanks!!! Re: call c/c++ function from VB

  1. #1
    Chris Guest

    Thanks!!! Re: call c/c++ function from VB


    Mark

    thanks for your help!!
    much appreciated, but I was receiving inconsistent results,
    probably more to do with the changes I made to the code you supplied me!

    anyway I cut my losses and wrote another small C function called
    format_postcode_A that calls the original function, ie

    int __stdcall format_postcode_A(char * the_post_code,
    char * post_code_two, char * the_post_code_three,
    char * area, char * dist, char * sect, char * unit)

    so that I can pass strings individually! makes more sense this way.
    this now works and I am a happy chap!

    thanks for the time you put in, you have increased my understanding of
    VB a little bit more!!!!

    regards

    chris


    "Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote:
    >"Chris" <vb.@127.0.0.1> wrote in message news:3d51042c$1@10.1.10.29...
    >>
    >> Hi all,
    >> I know this is not really a win32 question, but you are the
    >> group who will be most likely able to answer the question for me!!!
    >>
    >> How would I declare and call the following C/c++ function ithin VB,
    >> the function lives in one of my own dll's and has only needed to be called
    >> from C but there is now a need for it to be called from VB.
    >>
    >> int __stdcall format_postcode(char the_strings[7][20])
    >>
    >> As a C/c++ programmer, and only a novice VB programmer this is something
    >> that is new to me.
    >> any help would be much appreciated.
    >>

    >
    >Try the following. It is not very efficient, since 2D C byte arrays are

    not
    >compatible with 1D VB string arrays. Well, that's what I assume that you
    >want to do.
    >
    >----------------------------------------------------------------------------
    >--------------------------------------
    >
    >Private Sub Function CopyMemory Lib "Kernel32.dll" Alias "RtlMoveMemory"

    ( _
    > ByRef dest As Any, _
    > ByRef src As Any, _
    > ByVal copied As Long _
    >)
    >
    >Private Declare Function FormatPostcode Lib "<yourDLL.dll>" Alias
    >"format_postcode" ( _
    > ByRef the_strings As Byte _
    >) As Long
    >
    >Sub Test(ByRef asStrings())
    >' Actually, this could be very wrong, since I am not sure which dimension

    is
    >referenced first.
    >' If it is not this way, then reverse the order of the dimensions, and all
    >code that relies on it.
    >' Actually ... might be more complicated .. oh well.
    >Dim abytStrings(0 To 6, 0 To 19) As Byte
    >Dim lIndex As Long
    >Dim sTemp As String
    >
    > ' Convert the 1D VB string array into a 2D array of bytes.
    > ' Note the explicit Unicode -> ANSI translation.
    > For lIndex = 0 To 6
    > sTemp = StrConv(asStrings(lIndex), vbFromUnicode)
    > CopyMemory abytStrings(lIndex, 0), ByVal StrPtr(sTemp), LenB(sTemp)
    > Next lIndex
    >
    > ' Send a pointer to the first byte of the 2D array.
    > FormatPostcode abytString(0, 0)
    >
    > ' Copy byte array back into string array.
    > ' Note the explicit ANSI -> Unicode translation.
    > For lIndex = 0 To 6
    > sTemp = Space$(10) ' Half of 20
    > CopyMemory ByVal StrPtr(sTemp), abytStrings(lIndex, 0), LenB(sTemp)
    > asStrings(lIndex) = StrConv(sTemp, vbUnicode)
    > Next lIndex
    >
    >End Sub
    >
    >----------------------------------------------------------------------------
    >-----------------------------------------
    >
    >HTH
    >
    >--
    >Mark Alexander Bertenshaw
    >Programmer/Analyst
    >Chordiant Software, Inc.
    >Brentford
    >UK
    >
    >



  2. #2
    Mark Alexander Bertenshaw Guest

    Re: Thanks!!! Re: call c/c++ function from VB

    "Chris" <vb.@127.0.0.1> wrote in message news:3d525e19$1@10.1.10.29...
    >
    > Mark
    >
    > thanks for your help!!
    > much appreciated, but I was receiving inconsistent results,
    > probably more to do with the changes I made to the code you supplied me!
    >
    > anyway I cut my losses and wrote another small C function called
    > format_postcode_A that calls the original function, ie
    >
    > int __stdcall format_postcode_A(char * the_post_code,
    > char * post_code_two, char * the_post_code_three,
    > char * area, char * dist, char * sect, char * unit)
    >
    > so that I can pass strings individually! makes more sense this way.
    > this now works and I am a happy chap!
    >
    > thanks for the time you put in, you have increased my understanding of
    > VB a little bit more!!!!


    Chris -

    That's good to hear. But I wasn't really expecting it to work straight
    away - it was more of theoretical thing. As I said, I think that VB
    multidimensional arrays are implemented in a different way to C
    multidimensional arrays.
    Not all this is trial and error. It's a combination of MSDN Library, and a
    number of very good VB books. I suppose I really ought to mention "Advanced
    Visual Basic 6" by Matthew Curland ... again. But it is truly the best VB
    book for programmers who are not scared to get into the guts of VB and COM.
    The chapter on Arrays and the chapter on Strings is worth the price alone.

    --
    Mark Alexander Bertenshaw
    Programmer/Analyst
    Chordiant Software, Inc.
    Brentford
    UK




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center