DELPHI实现消息推送:个推C语言的DLL如何在DELPHI7中调用,我想知道如何转换,谢谢!以下是C的原形声明。

joky564913122 2015-06-08 09:43:12
#ifndef _I_GT_PUSH_H_
#define _I_GT_PUSH_H_

/***************************版本修改日志***************************
* ==========V1.0.1, chenjb, 2014-07-28==========
* 1. 去除pushMessageToListB接口,pushMessageToListA改名为pushMessageToList
* 2. pushMessageToList增加返回推送详情功能
*
* ==========V1.0.2, chenjb, 2015-01-14==========
* 1. curl bug修改,http://blog.chinaunix.net/uid-20761674-id-3391182.html
*/

#ifdef __cplusplus
extern "C" {
#endif

#if defined(WIN32) || defined(_WIN32)
# define STDCALL _stdcall
# if defined(GTPUSHSDK_EXPORTS)
# define DLL_EXTERN __declspec(dllexport)
# else
# define DLL_EXTERN __declspec(dllimport)
# endif
#else
# define DLL_EXTERN
# define STDCALL
#endif

// 调用返回结果结构体:SUCCESS(成功)/FAILED(失败)
typedef enum result_t {
SUCCESS = 0,
FAILED = 1,
ERR_ENCODE = 2
} Result;

// 单个结果键值对结构体
typedef struct entry_t {
char key[100];
char value[1024];
} Entry;

// 推送结果结构体
typedef struct i_push_result_t {
int size; // 结果中存在多少个Entry
Entry entry[10];
} IPushResult;

// 基本消息结构体
typedef struct message_t {
int isOffline;
long offlineExpireTime;
int priority;
} Message;

// 单个消息结构体
typedef struct single_message_t {
Message msg;
} SingleMessage;

// CID列表消息结构体
typedef struct list_message_t {
Message msg;
} ListMessage;

// 应用消息结构体
typedef struct app_message_t {
Message msg;
char **appIdList;
int appIdListSize;
char **phoneTypeList;
int phoneTypeListSize;
char **provinceList;
int provinceListSize;
char **tagList;
int tagListSize;
} AppMessage;

// 推送目标结构体
typedef struct target_t {
char *appId;
char *clientId;
} Target;

// 模板类型枚举
typedef enum template_type_t {
Transmission, PopupTransmission, NotyPopLoad, Notification, Link
} TemplateType;

// 应用于IOS手机
typedef struct push_info_t {
char *actionLocKey;
int badge;
char *message;
char *sound;
char *payload;
char *locKey;
char *locArgs;
char *launchImage;
} PushInfo;

// 基本模板结构体
typedef struct template_t {
char *appId;
char *appKey;
PushInfo pushInfo;
} Template;

// 透传模板结构体
typedef struct transmission_template_t {
Template t;
int transmissionType;
char *transmissionContent;
} TransmissionTemplate;

// 弹窗透传模板结构体
typedef struct popup_transmission_template_t {
Template t;
int transmissionType;
char *transmissionContent;
char *title;
char *text;
char *img;
char *confirmButtonText;
char *cancelButtonText;
} PopupTransmissionTemplate;

// 通知弹窗下载模板结构体
typedef struct noty_pop_load_template_t {
Template t;
char *notyIcon;
char *logoUrl;
char *notyTitle;
char *notyContent;
int isCleared;
int isBelled;
int isVibrationed;
char *popTitle;
char *popContent;
char *popImage;
char *popButton1;
char *popButton2;
char *loadIcon;
char *loadTitle;
char *loadUrl;
int isAutoInstall;
int isActived;
char *androidMark;
char *symbianMark;
char *iphoneMark;
} NotyPopLoadTemplate;

// 通知模板结构体
typedef struct notification_template_t {
Template t;
int transmissionType;
char *transmissionContent;
char *text;
char *title;
char *logo;
char *logoUrl;
int isRing;
int isVibrate;
int isClearable;
} NotificationTemplate;

// 链接模板结构体
typedef struct link_template_t {
Template t;
char *text;
char *title;
char *logo;
char *logoUrl;
char *url;
int isRing;
int isVibrate;
int isClearable;
} LinkTemplate;

// 推送结果详情
typedef struct push_detail_t {
char cid[33]; // 对应的CID
char ret[51]; // 详情内容
} PushDetail;

// 功能:推送初始化,程序运行前初始化一次即可
// 参数:
// host 个推服务器URL [in]
// appKey 个推申请应用的appKey [in]
// masterSecret 个推申请应用的masterSecret [in]
// testUTF8 本库所有接口必须传入字符必须为UTF-8编码,这里用于测试是否是UTF-8编码,固定填写"编码"两字 [in]
// 返回:Result枚举, SUCCESS、FAILED、ERR_ENCODE(编码测试失败)
DLL_EXTERN Result STDCALL pushInit(char *host, char *appKey, char *masterSecret, const char *testUTF8);

// 功能:初始化个推服务器鉴权
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// 返回:Result枚举, SUCCESS、FAILED
DLL_EXTERN Result STDCALL pushConnect(char *appKey);

// 功能:关闭个推服务器鉴权
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// 返回:Result枚举, SUCCESS、FAILED
DLL_EXTERN Result STDCALL pushClose(char *appKey);

// 功能:推送单条消息
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// msgData 单推消息结构体指针 [in]
// templateData 模板结构体指针 [in]
// templateType 模板类型 [in]
// target 推送目标结构体指针 [in]
// 返回:推送结果数据
DLL_EXTERN IPushResult STDCALL pushMessageToSingle(char *appKey, SingleMessage *msgData, void *templateData, TemplateType templateType, Target *target);

// 功能:获取contentId,用于pushMessageToListA接口
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// msgData CID列表消息结构体指针 [in]
// templateData 模板结构体指针 [in]
// templateType 模板类型 [in]
// contentId 用于返回contentId的指针 [out]
// size 可存放contentId的大小 [in]
// 返回:Result枚举, SUCCESS、FAILED
// 注意:如果size小于返回的ID,则返回FAILED
DLL_EXTERN Result STDCALL getContentId(char *appKey, ListMessage *msgData, void *templateData, TemplateType templateType, char *contentId, int size);

// 功能:取消contentId
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// contentId 需要取消的contentId [in]
// 返回:Result枚举, SUCCESS、FAILED
DLL_EXTERN Result STDCALL cancelContentId(char *appKey, char *contentId);

// 功能:推送CID列表
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// contentId 由getContentId返回的contentId [in]
// targetList 需要推送的目标列表 [in]
// targetSize 目标列表中有多少个Targe,建议每次50 [in]
// details 推送详情内容,不需要详情可填写NULL,否则需要预先分配足够内存(至少要targetSize个PushDetail) [in]
// 返回:Result枚举, SUCCESS、FAILED
DLL_EXTERN IPushResult STDCALL pushMessageToList(char *appKey, char *contentId, Target *targetList, int targetSize, PushDetail *details);

// 功能:推送应用消息
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// msgData 应用消息结构体指针 [in]
// templateData 模板结构体指针 [in]
// templateType 模板类型 [in]
// 返回:推送结果数据
DLL_EXTERN IPushResult STDCALL pushMessageToApp(char *appKey, AppMessage *msgData, void *templateData, TemplateType templateType);

// 功能:停止推送某个任务
// 参数:
// appKey 调用pushInit时APP对应的appKey [in]
// contentId 需要停止推送的contentId [in]
// 返回:Result枚举, SUCCESS、FAILED
DLL_EXTERN Result STDCALL pushStop(char *appKey, char *contentId);

// 功能:获取当前SDK版本号
// 参数:无
// 返回:版本号字符串
DLL_EXTERN const char * STDCALL getPushSdkVersion();

#ifdef __cplusplus
}
#endif

#endif
...全文
832 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjp1234airen4385 2015-06-09
  • 打赏
  • 举报
回复
这么长的东西,很少会有人细看的啊。 一般来说 char* 用string 就行, char** 用pchar
joky564913122 2015-06-09
  • 打赏
  • 举报
回复
就是怎么在DELPHI中定义相对应的函数啊。
joky564913122 2015-06-08
  • 打赏
  • 举报
回复
#include "IGtPush.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #include <windows.h> #endif using namespace std; static void printResult(IPushResult &result); static char *getUTF8(const char *in); static void releaseUTF8(const char *in); // 注意:接口传入字符必须为UTF-8编码,因ASCII字符UTF-8编码后与原先一样,所以可以不编码,但中文等非ASCII字符必须编码 // 如果返回的类似错误"post http data failed, code=6",错误码可百度CURL返回的错误码是什么意思,如http://www.cnblogs.com/wainiwann/p/3492939.html int main(int argc, char **argv) { IPushResult result = {0}; // SDK版本号 printf("Current SDK version: %s\n", getPushSdkVersion()); char *encode = getUTF8("编码"); // 初始化APP1 char *appId1 = "aK6jeksP5C7CsjSSEqLAA3"; char *appKey1 = "tpDVam96sY8pxhwBupJ4621"; char *masterSecret1 = "TBokfpttQJ6aHIhBE9y8671"; Result r1 = pushInit("http://sdk.open.api.igexin.com/apiex.htm", appKey1, masterSecret1, encode); if(r1!=SUCCESS){ printf("pushInit for app1 failed: ret=%d\n", r1); } // 初始化APP2 char *appId2 = "jo9UBGGZIi63FrxuYGD9vA"; char *appKey2 = "pjHdwGdihP8kECA0ba1u371"; char *masterSecret2 = "8SY6Bjf8fU9KEYokAh2tX31"; Result r2 = pushInit("http://sdk.open.api.igexin.com/apiex.htm", appKey2, masterSecret2, encode); if(r2!=SUCCESS){ printf("pushInit for app2 failed: ret=%d\n", r2); } releaseUTF8(encode); // 构建消息内容 Message msg = {0}; msg.isOffline = 1; msg.offlineExpireTime = 3600000; msg.priority = 1; SingleMessage singleMsg = {0}; singleMsg.msg = msg; // 模板内容 TransmissionTemplate templ = {0}; char *content1 = getUTF8("透传消息内容1"); templ.transmissionContent = content1; templ.transmissionType = 1; templ.t.appId = appId1; templ.t.appKey = appKey1; // 目标用户 Target target1 = {0}; target1.appId = appId1; target1.clientId = "2d5d1a37b50f843757fc85edb42ef17d"; // 推送消息 cout << "============pushMessageToSingle to APP1============" << endl; result = pushMessageToSingle(appKey1, &singleMsg, &templ, Transmission, &target1); printResult(result); // ##################以下结构同上面类似,用于推送消息给APP2的用户################## Target target2 = {0}; target2.appId = appId2; target2.clientId = "1624a0f884657f6c985b9beb5e4c432c"; TransmissionTemplate temp2 = {0}; char *content2 = getUTF8("透传消息内容2"); temp2.transmissionContent = content2; temp2.transmissionType = 1; temp2.t.appId = appId2; temp2.t.appKey = appKey2; cout << "============pushMessageToSingle to APP2============" << endl; result = pushMessageToSingle(appKey2, &singleMsg, &temp2, Transmission, &target2); printResult(result); releaseUTF8(content2); // ################################################################################ cout << "============pushMessageToList to APP1============" << endl; ListMessage listMsg = {0}; listMsg.msg = msg; char contentId[100] = {0}; ret = getContentId(appKey1, &listMsg, &templ, Transmission, contentId, sizeof(contentId)); if(ret == SUCCESS){ // 获取ContentId成功 Target targets[2] = {0}; targets[0].appId = "aK6jeksP5C7CsjSSEqLAA3"; targets[0].clientId = "d9e6617c66a6a022af8ea6cda2078dda"; targets[1].appId = "aK6jeksP5C7CsjSSEqLAA3"; targets[1].clientId = "cb24dc9c0aaa0c3208e5216be423fed7"; PushDetail details[2] = {0}; // 推送详情个数需要与目标个数相等 result = pushMessageToList(appKey1, contentId, targets, 2, details); printResult(result); cout << "*********************推送详情*********************" << endl; for(int i=0; i<2; i++){ cout << details[i].cid << ": " << details[i].ret << endl; } cout << "**************************************************" << endl; // 取消contentId ret = cancelContentId(appKey1, contentId); cout << "cancelContentId ret: " << (ret==SUCCESS? "SUCCESS" : "FAILED") << endl; } else { cout << "获取ContentId失败" << endl; } releaseUTF8(content1); return 0; } static void printResult(IPushResult &result) { cout << "print result:-------------" << endl; for (int i = 0; i < result.size; i++) { cout << result.entry[i].key << ": " << result.entry[i].value << endl; } cout << "print end:----------------" << endl; } //GBK编码转换到UTF8编码 static int GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen) { #ifdef WIN32 wchar_t * lpUnicodeStr = NULL; int nRetLen = 0; if(!lpGBKStr) //如果GBK字符串为NULL则出错退出 return 0; nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,NULL); //获取转换到Unicode编码后所需要的字符空间长度 lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间 nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码 if(!nRetLen) //转换失败则出错退出 return 0; nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,NULL); //获取转换到UTF8编码后所需要的字符空间长度 if(!lpUTF8Str) //输出缓冲区为空则返回转换后需要的空间大小 { if(lpUnicodeStr) delete []lpUnicodeStr; return nRetLen; } if(nUTF8StrLen < nRetLen) //如果输出缓冲区长度不够则退出 { if(lpUnicodeStr) delete []lpUnicodeStr; return 0; } nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL); //转换到UTF8编码 if(lpUnicodeStr) delete []lpUnicodeStr; return nRetLen; #else return 0; #endif } static char *getUTF8(const char *in){ #ifdef WIN32 int len = GBKToUTF8((unsigned char *)in, NULL, 0); char *out = new char[len+1]; GBKToUTF8((unsigned char *)in, (unsigned char *)out, len); return out; #else return (char *)in; #endif } static void releaseUTF8(const char *in){ #ifdef WIN32 delete[] in; #else #endif }

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧