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


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > .NET Framework
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    .NET Framework Ask general questions about the .NET Framework. Discuss general CTS, CLR, and other .NET Platform questions and issues

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old September 3rd, 2009, 03:02 PM
    dowhiletrue dowhiletrue is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 1
    dowhiletrue is an unknown quantity at this point (<10)
    Question Using PInvoke with function pointer and delegate

    Hi,

    I'm trying to implement the right definition in C# for the following C code from the FFmpeg shared library:

    void av_log_set_callback (void(*)(void *, int, const char *, va_list) callback)


    As its name fully explains, the function set a user callback function to be called when something is written in the log.
    Among the parameters there's a string (const char *) that must be formatted with a variable list of variant arguments (va_list).

    My C# implementation is this:

    public delegate void SetCallbackDelegate(IntPtr ptr, int level, string fmt, params object[] vargs);

    [DllImport(DLL_NAME, SetLastError = true)]
    public static extern void av_log_set_callback([MarshalAs(UnmanagedType.FunctionPtr)] SetCallbackDelegate callback);

    av_log_set_callback(new SetCallbackDelegate(LogCallback);

    private void LogCallback(IntPtr ptr, AVLogLevel level, string fmt, params object[] vargs)<br/>
    {
    // do something here<br/>
    }


    At runtime, whenever the callback function is being called, I got this error:

    InvalidVariant was detected
    Message: An invalid VARIANT was detected during a conversion from an unmanaged VARIANT to a managed object. Passing invalid VARIANTs to the CLR can cause unexpected exceptions, corruption or data loss.

    The problem is the last parameter, the va_list.
    Googlin' around I've found some posts that show some undocumented features of C#, including __arglist, that seems to be the right thing to use, but is not possible in a delegate definition.
    If I replace 'params object[]' with 'string[]' everything works fine, but the arguments aren't always strings.

    How should I do?

    Thanks in advance

    Marco
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > .NET Framework


    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 03:15 PM.



    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.