이 과정을 통해 배울 수 있는 것:
ActionScript와 통신하고 ANE 패키지로 준비된 Windows DLL 만들기.
1단계: C API 헤더 포함
이전 튜토리얼에서 만든 dllMain.cpp 파일을 엽니다.
먼저 Adobe AIR C API 헤더를 맨 위에 추가합니다.
1 | #include <FlashRuntimeExtensions.h> |
2단계: 확장 프로그램 이니셜 라이저 및 finailzer 추가
이것은 Windows 용 C++ 파일에서의 모습입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | extern "C" { __declspec(dllexport) void ExtensionInitializer(void** extData, FREContextInitializer* ctxInitializer, FREContextFinalizer* ctxFinalizer) { *ctxInitializer = &contextInitializer; // The name of function that will intialize the extension context *ctxFinalizer = &contextFinalizer; // The name of function that will finalize the extension context } __declspec(dllexport) void ExtensionFinalizer(void* extData) { return; } } |
우리는 C++로 코딩할 것이고 Windows용 AIR SDK는 C 구현으로 제공될 것입니다. ExtensionInitializer 및 ExtensionFinalizer 함수도 C++가 아닌 C로 구현됩니다. extern “C” 비트는 C 링키지를 사용하는 컴파일러에 대한 신호입니다.
3단계: 확장 컨텍스트 초기화 프로그램과 finailzer 추가
참고: 이 코드를 ExtensionInitializer 및 ExtensionFinalizer 함수 위에 추가하여 함수를 볼 수 있도록 하십시오.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | void contextInitializer( void * extData, const uint8_t * ctxType, FREContext ctx, uint32_t * numFunctionsToSet, const FRENamedFunction ** functionsToSet ) { // Create mapping between function names and pointers in an array of FRENamedFunction. // These are the functions that you will call from ActionScript - // effectively the interface of your native library. // Each member of the array contains the following information: // { function name as it will be called from ActionScript, // any data that should be passed to the function, // a pointer to the implementation of the function in the native library } static FRENamedFunction extensionFunctions[] = { { (const uint8_t*) "as_passAString", NULL, &ASPassAString } }; // Tell AIR how many functions there are in the array: *numFunctionsToSet = sizeof( extensionFunctions ) / sizeof( FRENamedFunction ); // Set the output parameter to point to the array we filled in: *functionsToSet = extensionFunctions; } |
1 2 3 4 | void contextFinalizer(FREContext ctx) { return; } |
이 그림은 Extension Context Initializer와 Finalizer가 기본 확장에 적합한 위치와 ANE가 사용될 때 호출 순서를 보여주는 다이어그램입니다.
4단계: 의미있는 코드 추가
드디어! ActionScript에서 문자열 매개 변수를 가져 와서 다시 전달하는 함수를 추가합시다. 이 코드를 contextInitializer 및 contextFinalizer 함수 위에 추가하십시오.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | FREObject ASPassAString( FREContext ctx, void* funcData, uint32_t argc, FREObject argv[] ) { // What this function does: reads a string passed in from ActionScript, // outputs it to the console, then sends it back to ActionScript. // This enumerator helps keep track of the parameters // we expect from ActionScript and their order. // Not technically necessary, but a good habit: // saves you havhavnig to remember which parameter you should access as argv[ 3 ]. enum { ARG_STRING_ARGUMENT = 0, ARG_COUNT }; // Another good habit, though not a requirement: // ARG_COUNT will have the value of the number of arguments you expect. // The assertion will fire (in a debug build) to tell you // if you mistakenly passed the wrong number of arguments // from ActionScritpt. assert( ARG_COUNT == argc ); // Read the ActionScript String object, packed here as a FREObject, // into a character array: uint32_t strLength = 0; const uint8_t * nativeCharArray = NULL; FREResult status = FREGetObjectAsUTF8( argv[ ARG_STRING_ARGUMENT ], &strLength, &nativeCharArray ); FREObject retObj; if ( ( FRE_OK == status ) && ( 0 < strLength ) && ( NULL != nativeCharArray ) ) { // Read the characters into a c string... std::string nativeString( ( const char * ) nativeCharArray ); // ...and output it into the console to see what we received: std::stringstream stros; stros << "This is the string we received from ActionScript: "; stros << nativeString; // Now let's put the characters back into a FREObject... FRENewObjectFromUTF8( strLength, nativeCharArray, &retObj ); } // ... and send them back to ActionScript: return retObj; } |
또한 파일 맨 위에 다음 include 지시문을 추가해야 합니다.
1 2 3 | #include <string> #include <assert.h> #include <sstream> |
5단계: DLL 빌드
그게 전부입니다. 이것이 Native Extension의 Windows 부분입니다. 빌드하면 (Visual C++를 사용하는 경우) 다음과 같은 내용이 표시됩니다.
1 2 3 4 5 | 1>------ Build started: Project: DiaDrawWindowsANETutorialDLL, Configuration: Debug Win32 ------ 1> dllMain.cpp 1> Creating library C:\Users\radoslava\dev\Windows ANE Template\NativeExtension\NativeLibrary\win\DiaDrawWindowsANETutorialDLL\Debug\DiaDrawWindowsANETutorialDLL.lib and object C:\Users\radoslava\dev\Windows ANE Template\NativeExtension\NativeLibrary\win\DiaDrawWindowsANETutorialDLL\Debug\DiaDrawWindowsANETutorialDLL.exp 1> DiaDrawWindowsANETutorialDLL.vcxproj -> C:\Users\radoslava\dev\Windows ANE Template\NativeExtension\NativeLibrary\win\DiaDrawWindowsANETutorialDLL\Debug\DiaDrawWindowsANETutorialDLL.dll ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== |
다음으로 넘어가십시오.
원문: http://easynativeextensions.com/windows-tutorial-the-native-dll-code/
최근 댓글