c# dll代码

CSharpLib.dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//  公共语言运行时支持 (/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;
}