C#类库(.NET Framework)
新建项目——>Visual C# ——>类库(.NET Framework)
C#类库代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Newtonsoft.Json;
namespace AliyunSmsClassLibrary
{
public class AliyunSms
{
string accessKeyId, secret;
public AliyunSms(string accessKeyId, string secret)
{
this.accessKeyId = accessKeyId;
this.secret = secret;
}
public void printAction()
{
Console.WriteLine("SendSms");
}
public string sendSms(string phoneNumber, string vehicleID, string Time, string location)
{
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, secret);
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "dysmsapi.aliyuncs.com";
request.Version = "2017-05-25";
request.Action = "SendSms";
// request.Protocol = ProtocolType.HTTP;
request.AddQueryParameters("PhoneNumbers", phoneNumber);
request.AddQueryParameters("SignName", "中交华安");
request.AddQueryParameters("TemplateCode", "SMS_177552111");
request.AddQueryParameters("TemplateParam", JsonConvert.SerializeObject(new { vehicleID, Time, location }));
try
{
CommonResponse response = client.GetCommonResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
return System.Text.Encoding.Default.GetString(response.HttpResponse.Content);
}
catch (ServerException e)
{
Console.WriteLine(e);
return e.Message;
}
catch (ClientException e)
{
Console.WriteLine(e);
return e.Message;
}
}
}
}
生成dll
在\bin\Debug\xx.dll
C++空项目
新建项目——>Visual C++ ——>空项目
公共语言运行时支持(/clr)
右键属性——>常规——>项目默认值——>公共语言运行时支持(/clr)
拷贝dll
将生成的dll文件拷贝到Debug目录下(如果是Release则拷贝到Release目录下)
C++源码
源文件——>添加——>新建项——>main.cpp
#include <string>
#include "assert.h"
#include <objbase.h>
#include <stdio.h>
#using "..\Debug\AliyunSmsClassLibrary.dll"
#pragma managed
using namespace AliyunSmsClassLibrary;
int main()
{
AliyunSmsClassLibrary::AliyunSms^ aliyunSms = gcnew AliyunSmsClassLibrary::AliyunSms("xxxxxx", "xxxxxx");
aliyunSms->printAction();
aliyunSms->sendSms("181xxxxxxxx", "闽B666", "11月22日 17点22分", "厦门软件园二期西门");
return 0;
}
运行
运行 ok