Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get started with CppSharp in a Win32 Dynamic Library like this ? #82

Closed
lizhengzhou opened this issue Oct 9, 2013 · 2 comments
Closed

Comments

@lizhengzhou
Copy link

一个.h文件DllDemo.h

ifdef DllDemo_EXPORTS

define DllAPI __declspec(dllexport)

else

define DllAPI __declspec(dllimport)

extern "C" //原样编译
{
DllAPI int __stdcall Max(int a,int b); //__stdcall使非C/C++语言内能够调用API
}

endif

在DllDemo.cpp文件中导入DllDemo.h文件,并实现Max(int,int)函数

include "DllDemo.h"

DllAPI int __stdcall Max(int a,int b)
{
if(a==b)
return NULL;
else if(a>b)
return a;
else
return b;
}

@mydogisbox
Copy link
Contributor

If you look in the GettingStarted documentation ( https://github.com/mono/CppSharp/blob/master/docs/GettingStarted.md ) there is an example of how to get started with CppSharp.

The basics are:

  1. Create a C# project and reference CppSharp
  2. Create a class which implements the ILibrary interface.
  3. Run the project and it will generate the necessary code
  4. Include that code in it's own C# project
  5. Reference that project to call C++ from C#!

Your implementation of ILibrary should look something like this:

class DllDemoGenerator : ILibrary
{
    static void Main(string[] args)
    {
        ConsoleDriver.Run(new DllDemoGenerator());
    }

    void Setup(Driver driver)
    {
        var options = driver.Options;
        options.GeneratorKind = LanguageGeneratorKind.CSharp;
        options.LibraryName = "DllDemo";
        options.Headers.Add("DllDemo.h");
        options.Libraries.Add("DllDemo.lib");
    }

    public void SetupPasses(Driver driver){}

    public void Preprocess(Driver driver, CppSharp.AST.Library lib){}

    public void Postprocess(CppSharp.AST.Library lib){}
}

If you have more questions you should check the documentation first.

@lizhengzhou
Copy link
Author

Thanks , this is Simpler than the GettingStarted documentation ~
Actually , I checked the documentation ……

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants