windows服务启动程序没有窗体的问题处理

这个问题,主要是服务是以system帐号启动的,在session0界面上,一般我们用户是在session1界面上,所以看不见窗体。

网上有说用Cjwdev.WindowsApi.dll 处理的, 这种方法明显时好时坏,也有网友说要创建一个没密码的用户才会成功,这明白很不安全,不适合服务器。

还有说有一个值守程序的,服务器启动时,值守程序未必已启动,也是一堆问题,后来Google几天,在要放弃时,看到一位大神的文章:

https://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-and-bit-Archite

https://www.codeproject.com/Articles/18775/Launch-your-app-into-session-using-a-service-hel

还有微软官方文章 :

https://code.msdn.microsoft.com/windowsapps/CSCreateProcessAsUserFromSe-b682134e

他的思想是,发现Winlogin这个进程是以seession1运行的,他就想到用这个进程的Token来启动程序,他成功了。但他这段代码没有判断,没有用户登录的情况,比如刚刚启动系统时,还是没有界面,这时要自已加段代码,判断一下当前有没有用户登录,没有用户登录用没界面的,有用户登录时,再Kill没界面进程,启动有界面的进程。

启动:

ApplicationLoader.PROCESS_INFORMATION procInfo;
ApplicationLoader.StartProcessAndBypassUAC(applicationName, out procInfo);

代码可以在他文章里下载,这里也贴出来:

public class ApplicationLoader
{
#region Structures

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int Length;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}

[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public int cb;
public String lpReserved;
public String lpDesktop;
public String lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}

#endregion

#region Enumerations

enum TOKEN_TYPE : int
{
TokenPrimary = 1,
TokenImpersonation = 2
}

enum SECURITY_IMPERSONATION_LEVEL : int
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3,
}

#endregion

#region Constants

public const int TOKEN_DUPLICATE = 0x0002;
public const uint MAXIMUM_ALLOWED = 0x2000000;
public const int CREATE_NEW_CONSOLE = 0x00000010;

public const int IDLE_PRIORITY_CLASS = 0x40;
public const int NORMAL_PRIORITY_CLASS = 0x20;
public const int HIGH_PRIORITY_CLASS = 0x80;
public const int REALTIME_PRIORITY_CLASS = 0x100;

#endregion

#region Win32 API Imports

[DllImport(“kernel32.dll”, SetLastError = true)]
private static extern bool CloseHandle(IntPtr hSnapshot);

[DllImport(“kernel32.dll”)]
static extern uint WTSGetActiveConsoleSessionId();

[DllImport(“advapi32.dll”, EntryPoint = “CreateProcessAsUser”, SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public extern static bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandle, int dwCreationFlags, IntPtr lpEnvironment,
String lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

[DllImport(“kernel32.dll”)]
static extern bool ProcessIdToSessionId(uint dwProcessId, ref uint pSessionId);

[DllImport(“advapi32.dll”, EntryPoint = “DuplicateTokenEx”)]
public extern static bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess,
ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType,
int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);

[DllImport(“kernel32.dll”)]
static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);

[DllImport(“advapi32″, SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]
static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);

#endregion

/// <summary>
/// Launches the given application with full admin rights, and in addition bypasses the Vista UAC prompt
/// </summary>
/// <param name=”applicationName”>The name of the application to launch</param>
/// <param name=”procInfo”>Process information regarding the launched application that gets returned to the caller</param>
/// <returns></returns>
public static bool StartProcessAndBypassUAC(String applicationName, out PROCESS_INFORMATION procInfo)
{
uint winlogonPid = 0;
IntPtr hUserTokenDup = IntPtr.Zero, hPToken = IntPtr.Zero, hProcess = IntPtr.Zero;
procInfo = new PROCESS_INFORMATION();

// obtain the currently active session id; every logged on user in the system has a unique session id
uint dwSessionId = WTSGetActiveConsoleSessionId();

// obtain the process id of the winlogon process that is running within the currently active session
Process[] processes = Process.GetProcessesByName(“winlogon”);
foreach (Process p in processes)
{
if ((uint)p.SessionId == dwSessionId)
{
winlogonPid = (uint)p.Id;
}
}

// obtain a handle to the winlogon process
hProcess = OpenProcess(MAXIMUM_ALLOWED, false, winlogonPid);

// obtain a handle to the access token of the winlogon process
if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, ref hPToken))
{
CloseHandle(hProcess);
return false;
}

// Security attibute structure used in DuplicateTokenEx and CreateProcessAsUser
// I would prefer to not have to use a security attribute variable and to just
// simply pass null and inherit (by default) the security attributes
// of the existing token. However, in C# structures are value types and therefore
// cannot be assigned the null value.
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.Length = Marshal.SizeOf(sa);

// copy the access token of the winlogon process; the newly created token will be a primary token
if (!DuplicateTokenEx(hPToken, MAXIMUM_ALLOWED, ref sa, (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, (int)TOKEN_TYPE.TokenPrimary, ref hUserTokenDup))
{
CloseHandle(hProcess);
CloseHandle(hPToken);
return false;
}

// By default CreateProcessAsUser creates a process on a non-interactive window station, meaning
// the window station has a desktop that is invisible and the process is incapable of receiving
// user input. To remedy this we set the lpDesktop parameter to indicate we want to enable user
// interaction with the new process.
STARTUPINFO si = new STARTUPINFO();
si.cb = (int)Marshal.SizeOf(si);
si.lpDesktop = @”winsta0\default”; // interactive window station parameter; basically this indicates that the process created can display a GUI on the desktop

// flags that specify the priority and creation method of the process
int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;

// create a new process in the current user’s logon session
bool result = CreateProcessAsUser(hUserTokenDup, // client’s access token
null, // file to execute
applicationName, // command line
ref sa, // pointer to process SECURITY_ATTRIBUTES
ref sa, // pointer to thread SECURITY_ATTRIBUTES
false, // handles are not inheritable
dwCreationFlags, // creation flags
IntPtr.Zero, // pointer to new environment block
null, // name of current directory
ref si, // pointer to STARTUPINFO structure
out procInfo // receives information about new process
);

// invalidate the handles
CloseHandle(hProcess);
CloseHandle(hPToken);
CloseHandle(hUserTokenDup);

return result; // return the result
}

}

 

支持Emoji表情图标在iOS与C#之间通讯

让C#支持Emoji表情图标存储入Mysql中,SqlServer没问题。

IOS5以上的版本表情已经从SoftBank改成Unified,Unified是占四个字节的,SoftBank是占三个字节。所以低版本的Mysql不支持,得

Mysql得5.5.3以上的版本才支持Utf8mb4字符集,只有Utf8mb4字符集才支持4个字节存储。

1:先改My.cnf文件

一般在这个地方: /etc/my.cnf,加上或修改成:

[client]
default-character-set = utf8mb4
 
[mysql]
default-character-set = utf8mb4
 
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

检查

MariaDB> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
+--------------------------+--------------------+
| Variable_name            | Value              |
+--------------------------+--------------------+
| character_set_client     | utf8mb4            |
| character_set_connection | utf8mb4            |
| character_set_database   | utf8mb4            |
| character_set_filesystem | binary             |
| character_set_results    | utf8mb4            |
| character_set_server     | utf8mb4            |
| character_set_system     | utf8               |
| collation_connection     | utf8mb4_unicode_ci |
| collation_database       | utf8mb4_unicode_ci |
| collation_server         | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0.01 sec)

如果不一样,要配到一样。

2:你的数据库的编码:

# For each database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;
# For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

这一句注意改三个地方:列名要对,列的类型要对,列的长度得一模一样。缺一不可。

然后你要修复一下表:

REPAIR TABLE table_name;
OPTIMIZE TABLE table_name;

3:调整你的C#代码,连接字符串加上一条:charset=utf8; 或者charset=utf8mb4; 

4:在你的插入或更新语句中的前面加上一条:

 

SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';

如此大功靠成,恭喜。

C#与C++代码互相调用

C# 托管代码与C++非托管代码互相调用

一. C# 中静态调用C++动态链接

 

1. 建立VC工程CppDemo,建立的时候选择Win32 Console(dll),选择Dll。

2. 在DllDemo.cpp文件中添加这些代码。

Code
extern “C” __declspec(dllexport) int Add(int a,int b)
{

return a+b;
}

3. 编译工程。

4. 建立新的C#工程,选择Console应用程序,建立测试程序InteropDemo
5. 在Program.cs中添加引用:using System.Runtime.InteropServices;

6. 在pulic class Program添加如下代码:

 

Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace InteropDemo
{
class Program
{
[DllImport(“CppDemo.dll”, EntryPoint = “Add”, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b); //DllImport请参照MSDN

static void Main(string[] args)
{
Console.WriteLine(Add(1, 2));
Console.Read();
}
}
}

好了,现在您可以测试Add程序了,是不是可以在C# 中调用C++动态链接了,当然这是静态调用,需要 将CppDemo编译生成的Dll放在DllDemo程序的Bin目录下

二. C# 中动态调用C++动态链接

在第一节中,讲了静态调用C++动态链接,由于Dll路径的限制,使用的不是很方便,C#中我们经常通过配置动态的调用托管Dll,例如常用的一 些设计模式:Abstract Factory, Provider, Strategy模式等等,那么是不是也可以这样动态调用C++动态链接呢?只要您还记得在C++中,通过LoadLibrary, GetProcess, FreeLibrary这几个函数是可以动态调用动态链接的(它们包含在kernel32.dll中),那么问题迎刃而解了,下面我们一步一步实验

1.  将kernel32中的几个方法封装成本地调用类NativeMethod

Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace InteropDemo
{
public static class NativeMethod
{
[DllImport(“kernel32.dll”, EntryPoint = “LoadLibrary”)]
public static extern int LoadLibrary(
[MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

[DllImport(“kernel32.dll”, EntryPoint = “GetProcAddress”)]
public static extern IntPtr GetProcAddress(int hModule,
[MarshalAs(UnmanagedType.LPStr)] string lpProcName);

[DllImport(“kernel32.dll”, EntryPoint = “FreeLibrary”)]
public static extern bool FreeLibrary(int hModule);
}
}

2. 使用NativeMethod类动态读取C++Dll,获得函数指针,并且将指针封装成C#中的委托。原因很简单,C#中已经不能使用指针了,如下
int hModule = NativeMethod.LoadLibrary(@”c:”CppDemo.dll”);

IntPtr intPtr = NativeMethod.GetProcAddress(hModule, “Add”);

详细请参见代码

Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace InteropDemo
{
class Program
{
//[DllImport(“CppDemo.dll”, EntryPoint = “Add”, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
//public static extern int Add(int a, int b); //DllImport请参照MSDN

static void Main(string[] args)
{
//1. 动态加载C++ Dll
int hModule = NativeMethod.LoadLibrary(@”c:\CppDemo.dll”);
if (hModule == 0) return;

//2. 读取函数指针
IntPtr intPtr = NativeMethod.GetProcAddress(hModule, “Add”);

//3. 将函数指针封装成委托
Add addFunction = (Add)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(Add));

//4. 测试
Console.WriteLine(addFunction(1, 2));
Console.Read();
}

/// <summary>
/// 函数指针
/// </summary>
/// <param name=”a”></param>
/// <param name=”b”></param>
/// <returns></returns>
delegate int Add(int a, int b);

}
}
原文链接:http://www.cnblogs.com/Jianchidaodi/archive/2009/03/09/1407270.html