using System; using System.Collections.Generic; using System.Text; // 마샬링을 위해 꼭!! 들어가야 하는 namespace입니다. using System.Runtime.InteropServices; namespace CSharp_Support { // kernel32.dll을 활용해서 unmanaged dll을 명시적으로 로드하기위한 클래스입니다. static class NativeMethods { [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string dllToLoad); [DllImport("kernel32.dll")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); [DllImport("kernel32.dll")] public static extern bool FreeLibrary(IntPtr hModule); } class Program { // SSDK의 sh_get_service_host 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr sh_get_service_host(string szUserID, string password, string serviceId); // SSDK의 sh_get_auth_string 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr sh_get_auth_string(string userId, string password, string serviceId , string authKey, string authfile, Int64 expire); // csdk의 sh_init 세번째 파라메터로 넘겨줄 함수포인터형 delegate선언. public delegate int sh_callback(IntPtr param, Int64 progress); // csdk의 sh_init 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr sh_init(string url, string authString, sh_callback proc, IntPtr param); // csdk의 sh_download 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int sh_download(IntPtr hcsdk, string srcPath, string dstPath, int writePolicy, Int64 availQuota); // csdk의 sh_free 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int sh_upload(IntPtr hcsdk, string srcPath, string dstPath, int writePolicy, Int64 availQuota); // csdk의 sh_get_error_number 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int sh_get_error_number(IntPtr hcsdk); // csdk의 sh_get_error_message 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr sh_get_error_message(IntPtr hcsdk); // csdk의 sh_free 함수의 타입을 정의하는 delegate선언. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void sh_free(IntPtr hcsdk); // 고객 가입시 안내되는 접속정보 및 cert파일 정보. // --> 기존에 서비스가 되고 있던 사이트로 알고 있습니다. // 각 변수에 적당한 값들을 넣어주시면 되겠습니다. // 혹, 정확한 입력값을 모르실경우 운영팀에 요청하시면 됩니다. /* public const string szService = ""; public const string szID = ""; public const string szPwd = ""; public const string szAuth = ""; public const string szCertPath = ""; */ public const string szService = "svc1bd04"; public const string szID = "solboxsvc1"; public const string szPwd = "5emffld)"; public const string szAuth = "svc1bd04"; public const string szCertPath = "D:/cert/solboxsvc1298.cert"; static void Main(string[] args) { //1. 제공된 sdk에서 테스트 구현에 필요한 함수들만 마샬링한다. // -> 제공된 sdk설명서를 참조하고 추가적으로 필요한 함수들은 // 아래 함수들처럼 마샬링처리 후에 사용하면된다. //SSdk.dll을 로드하기 위한 구문. IntPtr pSsdkDll = NativeMethods.LoadLibrary(@"SHSSDK.dll"); //CSdk.dll을 로드하기 위한 구문. IntPtr pCsdkDll = NativeMethods.LoadLibrary(@"SHCSDK.dll"); // ProcAddress를 얻어올 변수이다. // temporary 형태로 사용 할 수 있으므로, 한번 선언해서 계속 제사용 하면된다. IntPtr pAddressOfFunctionToCall; // NativeMethods를 활용해 dll에서 함수포인터를 얻는다. pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pSsdkDll, "sh_get_service_host"); // 취득한 함수 포인터를 Marshalling 단계를 거친다. sh_get_service_host sh_get_service_host = (sh_get_service_host)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_get_service_host)); pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pSsdkDll, "sh_get_auth_string"); sh_get_auth_string sh_get_auth_string = (sh_get_auth_string)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_get_auth_string)); pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pCsdkDll, "sh_init"); sh_init sh_init = (sh_init)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_init)); pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pCsdkDll, "sh_download"); sh_download sh_download = (sh_download)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_download)); pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pCsdkDll, "sh_get_error_number"); sh_get_error_number sh_get_error_number = (sh_get_error_number)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_get_error_number)); pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pCsdkDll, "sh_get_error_message"); sh_get_error_message sh_get_error_message = (sh_get_error_message)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_get_error_message)); pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pCsdkDll, "sh_free"); sh_free sh_free = (sh_free)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_free)); pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pCsdkDll, "sh_upload"); sh_upload sh_upload = (sh_upload)Marshal.GetDelegateForFunctionPointer( pAddressOfFunctionToCall, typeof(sh_upload)); // 2. 사용할 함수들에 대해 마샬링 처리를 한 후 sdk설명서를 참조해서 아래처럼 // 구현에 사용하면 된다. // 추가 설명 : 마샬링된 함수들의 파라메터들은 대부분 char* 또는 int, int64등과 // 같이 기본적은 변수 타입들이므로 적절한 변수타입으로 매칭시키면된다. // * 핸들과 pointer형 변수들은 IntPtr로 매칭하면 된다. // 호스트 정보를 얻어온다. string host = Marshal.PtrToStringAnsi(sh_get_service_host(szID, szPwd, szService)); Console.WriteLine(host); // c또는 c++에서 time(NULL)과 같은 값을 구하는 과정이다. DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = DateTime.Now; TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks); int recordTime = Convert.ToInt32(ts.TotalSeconds); // authstring을 얻어온다. string astring = Marshal.PtrToStringAnsi(sh_get_auth_string(szID, szPwd, szService, szAuth, szCertPath, recordTime + 86400)); // csdk를 사용하기 위한 초기화단계. IntPtr hcsdk = sh_init(host, astring, new sh_callback(CallbackProc), IntPtr.Zero); // 다운로드 테스트. // 2,3번째 다운로드 대상파일과 저장경로등은 적절히 변경하면된다. if (sh_download(hcsdk, "/SVC1_TEST/test_CSharp", "D:\\temp\\a1.xml", 1, -1) == 0) { Console.Write("sh_download ERROR :"); Console.Write(sh_get_error_number(hcsdk)); Console.Write(","); Console.WriteLine(Marshal.PtrToStringAnsi(sh_get_error_message(hcsdk))); } else { Console.WriteLine("sh_download SUCCESS\n"); } // 업로드 테스트. // 2,3번째 업로드 대상파일과 업로드경로등은 적절히 변경하면된다. if (sh_upload(hcsdk, "D:/test_10k.txt", "/SVC1_TEST/test_CSharp", 1, -1) == 0) { Console.Write("sh_upload ERROR :"); Console.Write(sh_get_error_number(hcsdk)); Console.Write(","); Console.WriteLine(Marshal.PtrToStringAnsi(sh_get_error_message(hcsdk))); } else { Console.WriteLine("sh_upload SUCCESS\n"); } // sh_init() 함수를 통해 얻은 sdk의 핸들은 꼭! 해제를 해야한다. sh_free(hcsdk); // NativeMethods 클래스를 사용해 로드한 sdk들을 해제한다. bool resultSSdk = NativeMethods.FreeLibrary(pSsdkDll); bool resultCSdk = NativeMethods.FreeLibrary(pCsdkDll); Environment.Exit(0); } // 콜백함수. __stdcall 형태로 콜링되도록 제공된다. public static int CallbackProc(IntPtr param, Int64 result) { Console.Write("Transfer : "); Console.WriteLine(result); return 1; } } }