Skip to content
Visual C++
2013.02.20 07:38

C# - etrade api site 게시물

조회 수 32885 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

 

 참고 : http://etrade.co.kr/index.jsp?url=%2Fxingapi%2Finfo%2FXingInfoOverview.jsp%3Fleft_menu_no%3D8%26front_menu_no%3D603

 

c#으로만든 API 래퍼 입니다.

접속, 로그인, 주문(매수, 매도) 까지만 테스트 완료 되었습니다.

필요한분 참고 하세요.

 

//

// 만든이 : 김국은 kbank@empal.com

// 배포   : 자유

// 배포시 배포자 정보 지우지 마세요.

//

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

 

namespace 이트레이드HTS

{

    public class XingApi

    {

        #region const

        public const int WM_APP = 0x8000;

        public const string 실서버 = "hts.etrade.co.kr";

        public const string 모의서버 = "mis.etrade.co.kr";

        public const int 서버포트 = 20001;

        #endregion

 

        #region enum

        public enum Xing메시지

        {

            연결끊김         = WM_APP + 1,

            데이터수신       = WM_APP + 3,

            실시간데이터수신 = WM_APP + 4,

            로그인         = WM_APP + 5,

            로그아웃        = WM_APP + 6,

            시간초과        = WM_APP + 7,

        }

 

        public enum Xing수신

        {

            요청데이터 = 1,

            메시지데이터 = 2,

            시스템오류데이터 = 3,

            릴리즈데이터 = 4,

        }

        #endregion

 

        static string 대표계좌번호 = null;

        static string 계좌비밀번호 = "0000";

        static IntPtr 윈도핸들 = IntPtr.Zero;

 

        public static string 비밀번호

        {

            get { return 계좌비밀번호; }

            set { 계좌비밀번호 = value; }

        }

 

        public static IntPtr 핸들

        {

            get { return 윈도핸들; }

            set { 윈도핸들 = value; }

        }

 

        #region 서버 연결 - 테스트 완료

        public static bool 연결(IntPtr 핸들, String 서버주소, int 포트, int 시작메시지)

        {

            윈도핸들 = 핸들;

            return ETK_Connect(핸들, 서버주소, 포트, 시작메시지, -1, -1);

        }

 

        public static bool 연결중인가

        {

            get { return ETK_IsConnected(); }

        }

 

        public static bool 연결종료()

        {

            return ETK_Disconnect();

        }

        #endregion

 

        #region 로그인 - 테스트 완료

        public static bool 로그인(String 아이디, String 암호, String 인증서암호, int 서버타입)

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_Login(윈도핸들, 아이디, 암호, 인증서암호, 서버타입, false);

        }

 

        public static bool 로그아웃()

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_Logout(윈도핸들);

        }

        #endregion

 

        #region 오류 - 테스트 완료

        public static int 오류코드

        {

            get { return ETK_GetLastError(); }

        }

 

        public static String 오류메시지(int 코드)

        {

            IntPtr 포인터 = Marshal.AllocHGlobal(512);

            ETK_GetErrorMessage(코드, 포인터, 512);

 

            String 메시지 = Marshal.PtrToStringAnsi(포인터);

            Marshal.FreeHGlobal(포인터);

 

            return 메시지;

        }

        #endregion

 

        #region 조회성TR 관련 - 테스트 완료

        public static int 요청(String Tr코드, IntPtr 데이터, int 데이터크기, bool 계속여부, String 다음키, int 시간초과)

        {

            if (IntPtr.Zero == 윈도핸들)

                return -1;

 

            return ETK_Request(윈도핸들, Tr코드, 데이터, 데이터크기, 계속여부, 다음키, 시간초과);

        }

 

        public static int 데이터요청(String 코드, IntPtr 데이터, int 데이터크기,

            byte 헤더타입, bool 압축여부, bool 암호와여부, bool 인증여부, bool 계속여부, int 시간초과)

        {

            if (IntPtr.Zero == 윈도핸들)

                return -1;

 

            return ETK_RequestData(윈도핸들, 코드, 데이터, 데이터크기, 헤더타입, 압축여부, 암호와여부, 인증여부, 계속여부, 시간초과);

        }

 

        public static int 데이터요청2(String 코드, IntPtr 데이터, int 데이터크기,

            int nAccPos, byte 헤더타입, String ServiceCode, bool 압축여부, bool 인증여부,

            bool bCert, bool 계속여부, String 다음키, int 시간초과)

        {

            if (IntPtr.Zero == 윈도핸들)

                return -1;

 

            return ETK_RequestDataEx(윈도핸들, 코드, 데이터, 데이터크기,

            nAccPos, 헤더타입, ServiceCode, 압축여부, 인증여부,

            bCert, 계속여부, 다음키, 시간초과);

        }

 

        public static bool 릴리즈요청데이터(int nRequestID)

        {

            return ETK_ReleaseRequestData(nRequestID);

        }

 

        public static bool 릴리즈메시지데이터(IntPtr lParam)

        {

            return ETK_ReleaseMessageData(lParam);

        }

        #endregion

 

        #region 실시간TR 관련

        public static bool 실시간데이터요청(String TrNo, String strData, int nDataUnitLen)

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_AdviseRealData(윈도핸들, TrNo, strData, nDataUnitLen);

        }

 

        public static bool 실시간데이터요청해지(String TrNo, String strData, int nDataUnitLen)

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_UnadviseRealData(윈도핸들, TrNo, strData, nDataUnitLen);

        }

 

        public static bool 실시간요청해지()

        {

            if (IntPtr.Zero == 윈도핸들)

                return false;

 

            return ETK_UnadviseWindow(윈도핸들);

        }

        #endregion

 

        #region 계좌 관련 - 테스트 완료

        public static int 계좌수

        {

            get { return ETK_GetAccountListCount(); }

        }

 

        public static bool 계좌정보(int iIndex, ref string 계좌번호)

        {

            IntPtr 포인터 = Marshal.AllocHGlobal(512);

            bool 성공 = ETK_GetAccountList(iIndex, 포인터, 512);

            if (true == 성공)

                계좌번호 = Marshal.PtrToStringAnsi(포인터);

            else

                계좌번호 = "";

            Marshal.FreeHGlobal(포인터);

 

            return 성공;

        }

        #endregion

 

        #region 정보얻기 - 테스트 완료

        public static String 통신미디어

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetCommMedia(포인터);

 

                String 미디어 = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return 미디어;

            }

        }

 

        public static String ETK미디어

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetETKMedia(포인터);

 

                String 미디어 = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return 미디어;

            }

        }

 

        public static String 로컬IP

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetClientIP(포인터);

 

                String ip = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return ip;

            }

        }

 

        public static String 서버이름

        {

            get

            {

                IntPtr 포인터 = Marshal.AllocHGlobal(512);

                ETK_GetServerName(포인터);

 

                String 이름 = Marshal.PtrToStringAnsi(포인터);

                Marshal.FreeHGlobal(포인터);

                return 이름;

            }

        }

        #endregion

 

        #region XingApi

        // 서버 연결

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Connect(IntPtr hWnd, String strServerIP, int iPort, int iStartMsgID, int iTimeout, int iSendMaxPacketSize);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_IsConnected();

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Disconnect();

 

        // 로그인

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Login(IntPtr hWnd, String strID, String strPwd, String CertPwd, int iType, bool bShowCertErrDlg);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_Logout(IntPtr hWnd);

 

        // 오류 메시지

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_GetLastError();

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_GetErrorMessage(int iErrorCode, IntPtr pBuffer, int iSize);

 

        // 조회성TR 관련

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_Request(IntPtr hWnd, String strTrCode, IntPtr pData, int iDataSize,

            [In, MarshalAs(UnmanagedType.Bool)] bool bNext, String strNextKey, int nTimeOut);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_RequestData(IntPtr hWnd, String strCode, IntPtr pData, int iDataSize,

            byte chHeaderType,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCompress,

            [In, MarshalAs(UnmanagedType.Bool)] bool bEncrypt,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCert,

            [In, MarshalAs(UnmanagedType.Bool)] bool bNext,

            int nTimeOut);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_RequestDataEx(IntPtr hWnd, String strCode, IntPtr pData, int iDataSize,

            int nAccPos, byte chHeaderType, String ServiceCode,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCompress,

            [In, MarshalAs(UnmanagedType.Bool)] bool bEncrypt,

            [In, MarshalAs(UnmanagedType.Bool)] bool bCert,

            [In, MarshalAs(UnmanagedType.Bool)] bool bNext,

            String NextKey, int nTimeOut);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_ReleaseRequestData(int nRequestID);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_ReleaseMessageData(IntPtr lParam);

 

        // 실시간TR 관련

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_AdviseRealData(IntPtr hWnd, String TrNo, String strData, int nDataUnitLen);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_UnadviseRealData(IntPtr hWnd, String TrNo, String strData, int nDataUnitLen);

 

        [DllImport("XingAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_UnadviseWindow(IntPtr hWnd);

 

        // 계좌 관련

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern int ETK_GetAccountListCount();

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        private static extern bool ETK_GetAccountList(int iIndex, IntPtr strAcc, int iAccSize);

 

        // 정보얻기

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetCommMedia(IntPtr pMedia);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetETKMedia(IntPtr pMedia);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetClientIP(IntPtr pClient);

 

        [DllImport("XingAPI.dll", CallingConvention = CallingConvention.StdCall)]

        private static extern void ETK_GetServerName(IntPtr pServerName);

        #endregion

    }

 

    #region 자료구조

    [StructLayout(LayoutKind.Sequential, Pack = 1), ComVisible(false)]

    public class 조회TR수신패킷

    {

        public int RequestID;                       // Request ID

        public int 받은데이터크기;              // 받은 데이터 크기

        public int 할당된크기;      // lpData에 할당된 크기

        public int 걸린시간;                // 전송에서 수신까지 걸린시간(1/1000초)

        public int nDataMode;                   // 1:BLOCK MODE, 2:NON-BLOCK MODE

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5 + 1)]

        public byte[] szTrCode;         // AP Code

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]

        public byte[] 다음조회;         // '0' : 다음조회 없음, '1' : 다음조회 있음

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 18 + 1)]

        public byte[] 연속키;           // 연속키, Data Header가 B 인 경우에만 사용

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30 + 1)]

        public byte[] 사용자데이터;         // 사용자 데이터

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]

        public byte[] Block명;          // Block 명, Block Mode 일때 사용

    }

 

    [StructLayout(LayoutKind.Sequential, Pack = 1), ComVisible(false)]

    public class 메시지수신패킷

    {

        public int RequestID;                       // Request ID

        public int nIsSystemError;              // 0:일반메시지, 1:System Error 메시지

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5 + 1)]

        public byte[] szMsgCode;                // 메시지 코드

        public int nMsgLength;                  // Message 길이

        [MarshalAs(UnmanagedType.LPStr)]

        public string 메시지;

    }

 

    [StructLayout(LayoutKind.Sequential, Pack = 1), ComVisible(false)]

    public struct 실시간TR수신패킷

    {

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3 + 1)]

        public byte[] szTrCode;

        public int nKeyLength;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 + 1)]

        public byte[] szKeyData;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 + 1)]

        public byte[] szRegKey;

        public int nDataLength;

    }

    #endregion

}

로그인 후 댓글쓰기가 가능합니다.

?

List of Articles
번호 분류 제목 날짜 조회 수
657 컴퓨터잡담 무선 공유기 선택시 중요한 4가지 2015.09.07 1543
656 컴퓨터잡담 핑테스트 프로그램 file 2015.09.01 1043
655 HTMLPHPMSQL PHP5.4.4 form 변수 전달받기(get, post방식) 2015.07.22 8329
654 [Docs]스프레드시트 구글 스프레드시트(Google Spreadsheet)를 데이터베이스로 활용하기 file 2015.07.20 5077
653 컴퓨터잡담 PC에 안드로이드 설치하기 1 file 2015.07.18 5441
652 Server 파일 업로드 폴더 변경 2015.07.13 1904
651 Server XE ckeditor 모바일에서 사용하기 2015.07.13 1151
650 [Docs]스프레드시트 구글드라이브 API file 2015.07.09 4541
649 Server XE 카카오톡 글전송 2015.07.08 1222
648 Server APMSETUP7 PHP 업그레이드 2015.06.02 6351
647 WindowsTip 윈도우 ip helper ipv6 도데체 무엇인가? 2015.05.11 4916
646 WindowsTip 캐논 MG 2990 스캔 드라이버 file 2015.05.03 4715
645 WindowsTip 캐논 프린터 MG2990 드라이버 file 2015.05.03 5300
644 WindowsTip Fasoo DRM 삭제하기(fph) file 2015.04.13 10591
643 WindowsTip UEFI 부팅+윈도우 8.1 2015.04.13 6218
642 WindowsTip 윈도우 8.1 부팅속도 개선방법 2015.04.13 4813
641 WindowsTip 윈도우 8.1 시작화면 아닌 바탕화면(데스크톱 모드)를 기본으로 설정하는 방법 file 2015.04.02 7557
640 WindowsTip 윈도우8.1 암호 입력없이 자동로그인 하기 2015.04.01 4359
639 WindowsTip 윈도우 8.1 업데이트 하지 않기 file 2015.03.21 3777
638 WindowsTip 인터넷 익스플로러 기본검색 공급자 설정해제 file 2015.03.21 4290
Board Pagination Prev 1 ... 12 13 14 15 16 ... 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소