C \ c ++ DLL-> C #-> maxscript calls the DLL written by C ++ through C #

Source: Internet
Author: User

It's actually the old post of a previous blog. If you haven't written a blog for too long, paste it.

Since max9, maxscript can directly call. Net DLL files and create classes to use the methods. And. Net can call the DLL file written in C ++. Therefore, you can use C # to encapsulate the C ++ code and call it in Max. The following is an example.

--------------- Below is DLL. Def

Library "DLL"

Exports
Helloworld @ 1

-------------- The following is the DLL. cpp File Content

Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Return true;
}

Void helloworld (
{
MessageBox (null, l "C ++ DLL", text ("in Win32 DLL"), mb_ OK );
}

------- The following is an encapsulation file of C # And compiled as importdll. dll.

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;

Namespace importdll
{
Public class win32dll
{
[Dllimport ("DLL. dll", entrypoint = "#1")]
Public static unsafe extern void helloworld ();
}
}

------------- Can then be called in maxscript

DOTNET. loadassembly (getfilenamepath (getsourcefilename () + "importdll. dll ")
(Dotnetclass "importdll. win32dll"). helloworld ()

------------------------

Here I used the serial number export method. If you do not want to use the def file to export the serial number, you can use the extern "c" to export the method in C language, this is because the exported Method Name of C ++ does not match the name in the DLL source file.

The only benefit of using. net instead of using maxsdk is that it does not have to be subject to version restrictions and does not need to be compiled for different versions. Of course, this method is only limited to extensions without any operation on Max. If the operation on Max is involved, you can only honestly use the SDK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.