c++ 调用c# dll修改win10锁屏背景
原创文章禁止转载
http://www.alom.com.cn/
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)
{
// Console.WriteLine(name);
var result = SetLockscreenImagePathAsync(name).ConfigureAwait(true);
int ret = result.GetAwaiter().GetResult();
return ret;
}
public static int SetLockscreenImagePathSync2(string name)
{
// Console.WriteLine(name);
var result = SetLockscreenImagePathAsync2(name).ConfigureAwait(true);
int ret = result.GetAwaiter().GetResult();
return ret;
}
public static int SetLockscreenImageUriSync(string uri)
{
// Console.WriteLine(name);
var result = SetLockscreenImageUriAsync(uri).ConfigureAwait(true);
int ret = result.GetAwaiter().GetResult();
return ret;
}
public static async Task<int> SetLockscreenImagePathAsync(string path)
{
//Console.WriteLine(path);
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
await LockScreen.SetImageFileAsync(file);
}
catch (Exception)
{
//Console.WriteLine(1);
return 1;
}
//Console.WriteLine("ok1");
return 0;
}
public static async Task<int> SetLockscreenImageUriAsync(string url)
{
//Console.WriteLine(path);
try
{
Uri imgUri = new Uri(url);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
await LockScreen.SetImageFileAsync(file);
}
catch (Exception)
{
//Console.WriteLine(1);
return 1;
}
//Console.WriteLine("ok1");
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)
{
//Console.WriteLine(1);
return 1;
}
//Console.WriteLine("ok");
return 0;
}
//async Task<bool> SetWallpaperAsync(string localAppDataFileName)
//{
// bool success = false;
// if (UserProfilePersonalizationSettings.IsSupported())
// {
// var uri = new Uri("ms-appx:///Local/" + localAppDataFileName);
// StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
// UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
// success = await profileSettings.TrySetLockScreenImageAsync(file);
// }
// return success;
//}
}
}
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
// 公共语言运行时支持 (/clr)
#include "pch.h"
#include "Lockscreen.h"
#using "CSharpLib.dll" //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();
// LockscreenWin10::LockscreenApi api;
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();
// LockscreenWin10::LockscreenApi api;
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();
// LockscreenWin10::LockscreenApi api;
int a = api->SetLockscreenImageUriSync(name);
delete[] szPath;
return a;
}