Marshal variable length arguments?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Marshal variable length arguments?

  1. #1
    Join Date
    Aug 2010
    Posts
    2

    Marshal variable length arguments?

    Hey,

    I am new to managed C++ and I have been struggling to find any information on this.

    Background:

    Need to develop a managed c++ "proxy" library to interface with a unmanaged c++ library so managed code can reuse same library. The unmanaged library is a logging library that has predefined format strings with variable arguments (of any kind). For example: Log 1 Description = "Unable to connect to %s. Connect tries: %d". We use vsprintf on these predefined strings with variable length arguments passed in to form the final log fields.

    When I pass in the variable arguments to the managed c++ code, everything looks fine, but when I pass it directly into the unmanaged function, the wrong values are appearing. Is there any special marshaling I am supposed to be doing to the data? Note that the variable arguments might not necessarily be the same type as each other (unless each predefined string is changed).

    So brief sudo code (omitted proper syntax):

    Log 1 Description = "Unable to connect to %s. Connect tries: %d".

    C# App:
    ManagedLogger.Log(LogId = 1, "1.1.1.1", 10)

    Managed C++ Lib:
    ManagedLogger::Log(int Id, ...array<Object^> ^ args)
    {
    UnmanagedLogger::Log(Id, args)
    }

    Unmanaged C++ Lib:
    UnmanagedLogger::Log(int Id, ...)
    {
    va_list argumentList;
    va_start()
    char* format = Log 1 Description;
    vsprintf(format, argumentList);
    va_end()
    }

    Any idea why my va_list is not getting the right data?

    Thanks!

  2. #2
    Join Date
    Jul 2002
    Posts
    2,494

    Re: Marshal variable length arguments?

    I wonder how such thing can be compiled. Log function, like vsprintf, expects some native arguments pushed on the stack. They don't have any idea what is array<Object^> ^.
    I think you need to do more work in the managed code, using String.Format Method (String, Object[]). Try to get format string from unmanaged Dll and convert it according to Format function requirements. Finally, pass formatted string to UnmanagedLogger for logging.

  3. #3
    Join Date
    Aug 2010
    Posts
    2

    Re: Marshal variable length arguments?

    Quote Originally Posted by Alex F View Post
    I wonder how such thing can be compiled. Log function, like vsprintf, expects some native arguments pushed on the stack. They don't have any idea what is array<Object^> ^.
    I think you need to do more work in the managed code, using String.Format Method (String, Object[]). Try to get format string from unmanaged Dll and convert it according to Format function requirements. Finally, pass formatted string to UnmanagedLogger for logging.
    Thanks, that's a great suggestion.

    I am still somewhat interested to see if there any way that the above would work. If not, no biggie. Anyone know?

    Thanks again.

  4. #4
    Join Date
    Jul 2002
    Posts
    2,494

    Re: Marshal variable length arguments?

    The problem in your code is that Object array is resolved at runtime, and C-slyle function call with variable number of arguments must be resolved at compile time. For example, to call printf, you need to place all arguments in the code, you cannot call such function with some array. .NET way is more flexible.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center