CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2
  • Learn How to Create Dual Mode Windows Services

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 3rd, 2009, 12:40 PM
    peeratep peeratep is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    peeratep is an unknown quantity at this point (<10)
    Comparison Data Type VB vs VC++

    C++ library:
    __declspec(dllexport) int WINAPI Caller(const char* username)

    Vb code:
    Private Declare Function Caller Lib "MyCaller.dll" () As Integer
    dim i as integer

    what! data type to by pass to caller function with correct using in c++
    'i = Caller ("user1")


    how to print log [const char* username] in c++, please suggest to me.
    Reply With Quote
      #2    
    Old November 3rd, 2009, 02:42 PM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,475
    hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+)
    Re: Comparison Data Type VB vs VC++

    Just and educated guess (been years since I've even seen VB code):
    Code:
    Private Declare Function Caller Lib "MyCaller.dll" (ByRef String) As Integer
    Reply With Quote
      #3    
    Old November 4th, 2009, 11:17 AM
    peeratep peeratep is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    peeratep is an unknown quantity at this point (<10)
    Re: Comparison Data Type VB vs VC++

    C++ library:
    __declspec(dllexport) int WINAPI Caller(const wchar_t* username)

    Vb code:
    Private Declare Function Caller Lib "MyCaller.dll" () As Integer

    in c++ library
    use const wchar_t*

    then in vb code what use


    Thank you.
    Reply With Quote
      #4    
    Old November 4th, 2009, 02:19 PM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,475
    hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+)
    Re: Comparison Data Type VB vs VC++

    I had a little free time on my lunch break so I decided to look into this.

    As I mentioned upthread, I'm not at all familiar with VB and haven't even looked at any VB code in nearly 10 years, so a real VB programmer is likely to be able to come up with a more elegant solution.

    First, the C++ dll needs the extern "C" declaration to prevent name mangling, so if you don't have the source for your DLL in question, you're probably screwed anyway.

    The only way I could get this to work was to declare the VB dll export as taking a short array as it's parameter. Then I had to find a way to actually send a VB string as a short array which resulted in the function ToCString shown below (this was done in VS2008 so YMMV):


    Code:
    Public Class Form1
        Private Declare Function Caller Lib "..\..\..\..\Debug\VBwDLL.dll" (ByVal s As Short()) As Integer
    
        Private Function ToCString(ByVal s As String) As Short()
            Dim i As Integer
            Dim x As Integer
            Dim sarr() As Short
    
            i = s.Length()
            ReDim sarr(i)
    
            For x = 0 To i - 1
                sarr(x) = AscW(s.Substring(x, 1))
            Next
            sarr(x) = 0
            Return sarr
    
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Long
    
            i = Caller(ToCString("Hello World"))
    
            TextBox1.Text = i.ToString()
        End Sub
    End Class
    I've attached the whole solution including the test dll and a VB forms app to call the dll if you want to check it out.
    Attached Files
    File Type: zip VBwDLL.zip (16.0 KB, 9 views)
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 10:53 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.