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

Thread: Wrapper for C++ function to be used in C# (as dll)

  1. #1
    Join Date
    Feb 2016
    Posts
    1

    Wrapper for C++ function to be used in C# (as dll)

    Hello,
    I want to use this function in my C# project:

    Code:
    typedef float3 vec; 
    class OBB { 
    public:     
    vec pos;vec r; vec axis[3];
    static OBB OptimalEnclosingOBB(const vec *pointArray, int numPoints);
    }
    So I need to write a wrapper that will go something like this:

    Code:
    using namespace System;
    using namespace msclr::interop;
    
    #if defined(__cplusplus)
    extern "C" {
    #endif
        __declspec(dllexport) ??? OEOBB_Wrapper(???, int numPoints);
    
    #if defined(__cplusplus)
    }
    #endif
    
    String^ OEOBB_Wrapper(float[][], int i) {
    // some code casting float[][] to vec *pointArray   
    // RerturnValue OptimalEnclosingOBB(const vec *pointArray, int numPoints);
    // casting ReturnValue into something C# can read
    return ??;
    }
    But I have 2 problems, The input: vec *pointArray type is not defined in C# so which type I can use instead and how do I cast it? The output: OBB class is not defined in C#, so what kind of return value my wrapper can use instead (after getting the OBB from the function)?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,280

    Re: Wrapper for C++ function to be used in C# (as dll)

    For pinvoke examples, take a look at the site: http://pinvoke.net

    The site has plenty of examples of pinvoking to c-style dll functions.

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




On-Demand Webinars (sponsored)