c# dll代码
CSharpLib.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Windows.Storage;
using Windows.System.UserProfile;
using Windows.Foundation.Metadata;
namespace LockscreenWin10
{
public class LockscreenApi
{
public static int SetLockscreenImagePathSync(string name)
{
var result = SetLockscreenImagePathAsync(name).ConfigureAwait(true);
int ret = result.GetAwaiter().GetResult();
return ret;
}
public static int SetLockscreenImagePathSync2(string name)
{
var result = SetLockscreenImagePathAsync2(name).ConfigureAwait(true);
int ret = result.GetAwaiter().GetResult();
return ret;
}
public static int SetLockscreenImageUriSync(string uri)
{
var result = SetLockscreenImageUriAsync(uri).ConfigureAwait(true);
int ret = result.GetAwaiter().GetResult();
return ret;
}
public static async Task<int> SetLockscreenImagePathAsync(string path)
{
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
await LockScreen.SetImageFileAsync(file);
}
catch (Exception)
{
return 1;
}
return 0;
}
public static async Task<int> SetLockscreenImageUriAsync(string url)
{
try
{
Uri imgUri = new Uri(url);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
await LockScreen.SetImageFileAsync(file);
}
catch (Exception)
{
return 1;
}
return 0;
}
public static async Task<int> SetLockscreenImagePathAsync2(string path)
{
var dirPath = Path.GetDirectoryName(path);
var fileName = Path.GetFileName(path);
Console.WriteLine(path);
try
{
var folder = await StorageFolder.GetFolderFromPathAsync(dirPath);
var imgFile = await folder.GetFileAsync(fileName);
using (var stream = await imgFile.OpenAsync(FileAccessMode.Read))
{
await LockScreen.SetImageStreamAsync(stream);
}
}
catch (Exception)
{
return 1;
}
return 0;
}
}
}
c++ 调用代码
Lockscreen.h
#pragma once
#include <string>
class Lockscreen
{
public:
static int SetImagePath(const std::string& path);
static int SetImageUri(const std::string& path);
static int SetImagePath2(const std::string& path);
};
Lockscreen.cpp
#include "pch.h"
#include "Lockscreen.h"
#using "CSharpLib.dll"
#include <iostream>
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;
using namespace LockscreenWin10;
int Lockscreen::SetImagePath(const std::string& path)
{
char * szPath = new char[path.length() + 1];
memcpy(szPath, path.c_str(), path.length());
szPath[path.length()] = 0;
String^ name = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)szPath);
LockscreenWin10::LockscreenApi^ api = gcnew LockscreenWin10::LockscreenApi();
int a = api->SetLockscreenImagePathSync(name);
delete[] szPath;
return a;
}
int Lockscreen::SetImagePath2(const std::string& path)
{
char * szPath = new char[path.length() + 1];
memcpy(szPath, path.c_str(), path.length());
szPath[path.length()] = 0;
String^ name = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)szPath);
LockscreenWin10::LockscreenApi^ api = gcnew LockscreenWin10::LockscreenApi();
int a = api->SetLockscreenImagePathSync2(name);
delete[] szPath;
return a;
}
int Lockscreen::SetImageUri(const std::string& path)
{
char * szPath = new char[path.length() + 1];
memcpy(szPath, path.c_str(), path.length());
szPath[path.length()] = 0;
String^ name = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)szPath);
LockscreenWin10::LockscreenApi^ api = gcnew LockscreenWin10::LockscreenApi();
int a = api->SetLockscreenImageUriSync(name);
delete[] szPath;
return a;
}
|