C++ Wrapper for VB.net
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: C++ Wrapper for VB.net

  1. #1
    Join Date
    Oct 2010
    Posts
    43

    Question C++ Wrapper for VB.net

    Hello Guru's:

    I am trying to create a wrapper for C++ dll using a vb.net. I have manage to create a wrapper but upon calling the function
    I have encounter a error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

    Below is my code in importing the c++ dll


    Code:
    Imports System.Runtime.InteropServices
    
    Public Class MYWrapper    
        <DllImport("MYtest.dll", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi)>
        Public Shared Sub Reset(ByVal sSection As [String], ByVal lResult As Int32)
        End Sub    
        <DllImport("MYtest.DLL", EntryPoint:="Init", SetLastError:=True, _
            CharSet:=CharSet.Ansi, ExactSpelling:=True, _
            CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Sub Init(ByVal objectInstanceName As String, ByVal sPath As String, ByVal sSection As String, ByVal sTestPlanRevision As String, ByRef lResult As Int32)
        End Sub
    End Class
    C++ Header
    Code:
    extern "C" MY_API void Reset  (const char *sSection, long &lResult);
    extern "C" MY_API void Init     (const char *sObjectInstanceName, const char *sPath, const char *sSection, const char *sTestplanRevision, long &lResult);

    VB.net Program
    Code:
     Dim IResult As Int32
    
            Const sOjectname As String = "C:\confg.cfg"
            Dim sError As String
            Const sProductConf As String = "Config.cfg"
            Const sOperation As String = "TEST0001"
            Const sTPRev As String = "V1"
    
            Try
                MYWrapper.Wrapper.Init(sOjectname, sProductConf, sOperation, sTPRev, IResult)
            Catch ex As Exception
                sError = ex.Message.ToString()
            End Try
    Regards,
    Newbie here
    Last edited by kpax1684; August 29th, 2014 at 07:55 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: C++ Wrapper for VB.net

    Please, edit your post to add the Code tags around the code snippets. Otherwise your code is absolutely unreadable!
    Victor Nijegorodov

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,480

    Re: C++ Wrapper for VB.net

    Do the dll functions work when called from a C++ test project?

  4. #4
    Join Date
    Oct 2010
    Posts
    43

    Re: C++ Wrapper for VB.net

    Hello Arjay:

    YES is 100% working when it is being called in the C++ test project.. I don't have the source code of this c++ dll, I only have the DLL and a .H header.



    Regards,
    Newbie here

    Quote Originally Posted by Arjay View Post
    Do the dll functions work when called from a C++ test project?

  5. #5
    Join Date
    Oct 2010
    Posts
    43

    Re: C++ Wrapper for VB.net

    @VictorN:

    Sorry if you could not read my code., I don't know how to put my code around the code snippets.

    Quote Originally Posted by VictorN View Post
    Please, edit your post to add the Code tags around the code snippets. Otherwise your code is absolutely unreadable!

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: C++ Wrapper for VB.net

    Quote Originally Posted by kpax1684 View Post
    @VictorN:

    Sorry if you could not read my code., I don't know how to put my code around the code snippets.
    Hmm, being almost four years here in Forum but not having read the Announcement: Before you post.... yet?
    Victor Nijegorodov

  7. #7
    Join Date
    Oct 2010
    Posts
    43

    Re: C++ Wrapper for VB.net

    Thanks, VictorN for reminding me.., I tried to make it colorful but this is i've got..,

    Quote Originally Posted by VictorN View Post
    Hmm, being almost four years here in Forum but not having read the Announcement: Before you post.... yet?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: C++ Wrapper for VB.net

    Quote Originally Posted by kpax1684 View Post
    Thanks, VictorN for reminding me.., I tried to make it colorful but this is i've got..,
    it's OK now. To be readable code has to be formatted with proper tabs7indentations and whitespaces. Colorisation is not so important!
    Victor Nijegorodov

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,480

    Re: C++ Wrapper for VB.net

    Check out the pinvoke.net site.

  10. #10
    Join Date
    Oct 2010
    Posts
    43

    Re: C++ Wrapper for VB.net

    I have checked the pinvoke site but I could not get a better explanation there..,

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,480

    Re: C++ Wrapper for VB.net

    Quote Originally Posted by kpax1684 View Post
    I have checked the pinvoke site but I could not get a better explanation there..,
    How you use the pinvoke.net site is you find a signature that is correct for your function prototype. There are many examples of Win32 prototypes so you should be able to find one similar to what you need.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center