CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2011
    Posts
    1

    Angry return and reuse HANDLE from unmanaged c++ code

    Using VB .NET 4.0 , I'm trying to run through some c++ unmanaged code..

    The issue is that one function returns a HANDLE, and another needs the same HANDLE ref. I can tell the initial function is running successfully, but afterwards, the IntPtr is set to 0 (blank).

    Naturally, it's driving me crazy, since this is the first time I've really had to work with P/Invoke. Unfortunately, I'm not sure what the HANDLE points too, since it's being returned from a file in an old compiled lib

    Can * anyone * help with this? I've included the code below.


    The unmanaged C++ code:
    Code:
    int log_connect(char* node, UINT dte, HANDLE* ptr_log)
    {
       HANDLE    hLog = NULL;
       int       rc = 0;
    
       rc = EmEvt_logOpen( node, dte, EMEVT_LOG_MODE_READ, &hLog );
    
    
       ptr_log = &hLog;
    
       return rc;
    }
    
    int log_read(HANDLE* ptr_log, EMEVT_LOG* ptr_evt)
    {
    
    	int rc = 0;
    	rc = EmEvt_logRead( *ptr_log, 0, *ptr_evt );
    
    	return rc;
       
    }

    My managed vb .net code (console app):
    Code:
     <DllImport("em_utilities.dll", CharSet:=CharSet.Auto)> _
        Public Function log_connect(<MarshalAs(UnmanagedType.LPStr)> ByVal node As String, <MarshalAs(UnmanagedType.U4)> ByVal dte As UInteger, ByRef ptr_log As IntPtr) As Integer
        End Function
    
    <DllImport("em_utilities.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function log_read(ByRef ptr_log As IntPtr) As Integer
            End Function
    Code:
    'Get today's date in requested format
    Dim dte As UInteger = 10000 * DateTime.Today.Year + 100 * DateTime.Today.Month.ToString() + DateTime.Today.Day
    
    Dim log As IntPtr
    
    log_connect("server", dte, log)
    
    If log = IntPtr.Zero Then
        System.Console.WriteLine("IntPtr blank")
    End If
    
    log_read(log)

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: return and reuse HANDLE from unmanaged c++ code

    Some issues with your code :

    Quote Originally Posted by dwuser22 View Post
    Code:
    int log_connect(char* node, UINT dte, HANDLE* ptr_log){
    int, may be equivalent to the short data type, so use short ineatd of Integer in VB

    Quote Originally Posted by dwuser22 View Post
    Code:
    Dim log As IntPtr '-- > Here you are declaring the IntPtr
    
    log_connect("server", dte, log) '-->Here you are using it, it still being ZERO
    
    If log = IntPtr.Zero Then '--> It is obviously still Zero
    Last edited by HanneSThEGreaT; May 25th, 2011 at 09:17 AM.

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
  •  





Click Here to Expand Forum to Full Width

Featured