Unity용 Windows C++ 소스 코드 플러그인declspec 필요 없음

11100 단어 Unitytech

개시하다


Unity에서 Scripting Backend를 il2cpp로 만들면 C++의 원본 파일을 Plugins 폴더에 직접 저장해서 사용할 수 있습니다.
  • Windows/macOS는 2018.1 이후[1][2]
  • 안드로이드는 2018.2 이후[3]
  • iOS 5.2 이후[4]
  • 여기서 Windows C++ 소스 코드 플러그인에 대한 설명과 예제 코드는 다음과 같습니다.
    함수는 생성된 C++ 코드로 연결되어 있기 때문에P/Invoke에 DLL 추가 없음
    extern "C" __declspec(dllexport) int __stdcall CountLettersInString(wchar_t* str)
    {
        int length = 0;
        while (*str++ != L'\0')
            length++;
        return length;
    }
    
    그러나 통일된 링크가 있으면 외부에 호출되지 않기 때문에 선언할 필요가 없다.
    그래서 우리는 정말 발표하지 않아도 되는지 검증했다.

    __declspec(dllexport)과 구축이 있습니다


    문서에 있는 같은 C++ 소스 코드 플러그인 [1:1] 과 함께 프로그램을 만들고 이 프로그램을 실행하면 매개 변수 문자열의 길이를 되돌려줍니다.
    NewBehaviourScript.cs
    [DllImport("__Internal")]
    private static extern int
    CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);
    
    NativeFunctions.cpp
    extern "C" __declspec(dllexport) int __stdcall CountLettersInString(wchar_t* str)
    {
        int length = 0;
        while (*str++ != L'\0')
            length++;
        return length;
    }
    
    그러면 구축된 GameAssiembly입니다.dll의 정의는 어떻습니까?
    >dumpbin /exports GameAssembly.dll | findstr CountLettersInString
              2    1 00109360 CountLettersInString = CountLettersInString
    
    dumpbin으로 확인한 후 C++ 소스 코드 플러그인에 정의된 함수를 내보냈습니다.

    __declspec(dllexport) 구축 없음


    이번에는 아래 __declspec(dllexport) 만 삭제했고, 구축을 실행하는 프로그램도 매개 변수 문자열의 길이를 되돌려줍니다.
    NativeFunctions.cpp
    extern "C" int __stdcall CountLettersInString(wchar_t* str)
    {
        int length = 0;
        while (*str++ != L'\0')
            length++;
        return length;
    }
    
    아까랑 똑같아.dll의 정의를 확인한 후 이번에는 존재하지 않습니다.
    >dumpbin /exports GameAssembly.dll | findstr CountLettersInString
    
    그렇다면 구축시__declspec(dllexport)에 출력된 원본 코드를 찾으면<ProductName>_ButDontShipItWithYourGame 다음 함수Assembly-CSharp.cpp를 호출합니다.
    C++ 소스 코드 플러그인CountLettersInString도 이 폴더에 함께 출력되기 때문에 발표하지 않아도 함수NativeFunctions.cpp를 호출할 수 있습니다.
    _ButDontShipItWithYourGame\il2cppOutput\Assembly-CSharp.cpp
    // System.Int32 NewBehaviourScript::CountLettersInString(System.String)
    IL2CPP_EXTERN_C IL2CPP_METHOD_ATTR int32_t NewBehaviourScript_CountLettersInString_mED52AF1B6313A9C16FA9E483C3444661B539A17E (String_t* ___str0, const RuntimeMethod* method)
    {
    	typedef int32_t (DEFAULT_CALL *PInvokeFunc) (Il2CppChar*);
    
    	// Marshaling of parameter '___str0' to native representation
    	Il2CppChar* ____str0_marshaled = NULL;
    	if (___str0 != NULL)
    	{
    		____str0_marshaled = ___str0->get_address_of_m_firstChar_1();
    	}
    
    	// Native function invocation
    	int32_t returnValue = reinterpret_cast<PInvokeFunc>(CountLettersInString)(____str0_marshaled);
    
    	return returnValue;
    }
    

    총결산


    Windows에서는 일2cpp의 C++ 소스 플러그인 __declspec(dllexport) 을 발표하지 않아도 문제없이 호출할 수 있습니다.
    이에 따라 해독 대책 등을 통해 분석 단서를 줄이는 수단 중 하나다.
    각주
    https://docs.unity3d.com/2018.1/Documentation/Manual/WindowsPlayerCPlusPlusSourceCodePluginsForIL2CPP.html ↩︎ ↩︎
    https://docs.unity3d.com/2018.1/Documentation/Manual/macOSPlayerCPlusPlusSourceCodePluginsForIL2CPP.html ↩︎
    https://docs.unity3d.com/2018.2/Documentation/Manual/AndroidNativePlugins.html ↩︎
    https://docs.unity3d.com/520/Documentation/Manual/PluginsForIOS.html ↩︎

    좋은 웹페이지 즐겨찾기