User32 Windows UI Api on Universal Windows Platform - api

I don't have a lot of experience on UWP programming and i built an app does some stuff on Bluetooth. Now i have to cary some info through APIs. But somehow "SendMessage" doesnt work. Here is the code i'm using
public static class ApiComm
{
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
public static string DelphiFormClassName = "TFrmMain";//"wsdk_delphi_api_test";
public static string DelphiFormCaptionName = "Deneme";
private static IntPtr FindDelphiForm(string delphiFormClassName, string delphiFormCaptionName)
{
return FindWindow(delphiFormClassName, delphiFormCaptionName);
}
private static IntPtr FindDelphiForm()
{
return FindDelphiForm(DelphiFormClassName, DelphiFormCaptionName);
}
public static int TextToID(string text)
{
int mu = (text == null ? 0 : text.Length);
int result = 0;
for (int i = 0; i < mu; i++)
result = result + (((i + 1) * 256) + (byte)(text[i]));
return result;
}
private const int WM_COMMAND = 0x0111;
private const int StartFrom = 500;
private static int EventID(RemoteDeviceUpdate anEvent)
{
return StartFrom + (int)anEvent;
}
public static void SendInfo(int remoteID, RemoteDeviceUpdate anEvent)
{
IntPtr wP = FindDelphiForm();
if (wP != null && wP != IntPtr.Zero)
{
int eID = EventID(anEvent);
SendMessage(wP, WM_COMMAND, eID, remoteID);
}
}
public static void SendInfo(string remoteID, RemoteDeviceUpdate anEvent)
{
SendInfo(TextToID(remoteID), anEvent);
}
}
when i try SendInfo with another .net projects, it works just fine but with UWP it doesnt (and it doesnt give any error also)
do i have to add packages or something to my project..
any help would be appreciated, thanks.

Derive from this case, you need check if the API available to Windows Runtime apps.
Apps can p-invoke DLLs deployed with the app package. The DLL will run in the app's context and have the same API access as the app itself. See Win32 and COM APIs for an overview of Win32 API available to Windows Runtime apps.
If you just want to build an app does some stuff on Bluetooth. You can do it all with uwp
Bluetooth api. And this is official document. For RFFCOMM, this is uwp code sample that you could refer.

Related

How to copy file from network share in VB for an windows ce application

I am doing an application using vb.net.
What I need is access my computer (win 8) from a windows ce device and copy one file to the windows ce device.
I already did that but what I need now is an way to pass the user, password and domain.
I have researched about and found some solutions using System.Security.WindowsImpersonationContext So I think Something similar to that that will work in a windows ce application.
SOrry If you don't get something I have said but I am new on programming and english is not my home language.
Thanks in advance for your help
You could use the MapDrive method of the Network class from the SDF. Since it's pretty straightforward, the source for that method is as follows (I leave it to you to get it to VB):
public static void MapDrive(IntPtr hwnd, string netRes, string shareName, string userName, string password)
{
NETRESOURCE NetRes = new NETRESOURCE();
NetRes.dwScope = RESOURCE_GLOBALNET | RESOURCE_REMEMBERED;
NetRes.dwType = RESOURCETYPE_DISK;
NetRes.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
NetRes.dwUsage = RESOURCEUSAGE_CONNECTABLE;
NetRes.lpRemoteName = Marshal2.StringToHGlobalUni(netRes);
NetRes.lpLocalName = Marshal2.StringToHGlobalUni(shareName);
NetRes.lpComment = IntPtr.Zero;
NetRes.lpProvider = IntPtr.Zero;
int ret = WNetAddConnection3(hwnd, NetRes, password, userName, 1);
if (ret != 0)
{
throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString());
}
}
private class NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public IntPtr lpLocalName;
public IntPtr lpRemoteName;
public IntPtr lpComment;
public IntPtr lpProvider;
}
[DllImport("coredll.dll")]
private static extern int WNetAddConnection3(
IntPtr hwndOwner,
NETRESOURCE lpNetResource,
string lpPassword,
string lpUserName,
int dwFlags);
const int RESOURCE_GLOBALNET = 0x00000002;
const int RESOURCE_REMEMBERED = 0x00000003;
const int RESOURCETYPE_DISK = 0x00000001;
const int RESOURCEDISPLAYTYPE_SHARE = 0x00000003;
const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;

WinAPI SendMessage from .NET

I have an example of winapi code:
struct CommunicationInfo {
long internalMsg;
const TCHAR * srcModuleName;
void * info;
};
...
const TCHAR* szText = _T("Hello from my plugin!\n(test message)");
CommunicationInfo ci = { 0x0401, cszMyPlugin, (void *) szText };
::SendMessage( hNppWnd, 0x111, (WPARAM) _T("NppExec.dll"), (LPARAM) &ci );
I want make the same call from .net and i wrote such wrapper:
[StructLayout(LayoutKind.Sequential)]
public struct CommunicationInfo
{
public Int64 internalMsg;
[MarshalAs(UnmanagedType.LPWStr)]
public StringBuilder srcModuleName;
[MarshalAs(UnmanagedType.LPWStr)]
public StringBuilder data;
};
...
[DllImport("user32")]
public static extern IntPtr SendMessage(IntPtr hWnd,
NppMsg Msg, IntPtr wParam,
[MarshalAs(UnmanagedType.Struct)] CommunicationInfo communicationInfo);
...
SendMessage(hNppWnd, 0x111,
Marshal.StringToHGlobalUni("NppExec.dll"),
new CommunicationInfo
{
data = new StringBuilder("test test"),
internalMsg = 0x0401,
srcModuleName = new StringBuilder("ModuleName")
});
But this code doesn't work. Where did I make a mistake ?
"long" field in CommunicationInfo struct is 32-bit in WinAPI, I believe. So try defining "internalMsg" as System.Int32 in C#
To be sure, try calling printf("%d\n", sizeof(CommunicationInfo)) in C/C++ to know the actual size. If it is (4 + 4 + 4) on a 32-bit system, then the C# struct must also be of 12 byte size.
The "char*" pointer must also be the pointer to unmanaged memory, so the StringBuilder just won't do.
See this PInvoke error when marshalling struct with a string in it for the marshalling sample
As Viktor points out, C/C++ long is 32 bits in size so needs to be matched with C# int. On top of that, the passing of the struct is not handled correctly. In addition the call to StringToHGlobalUni leaks since you never call FreeHGlobal.
I'd probably handle the marshalling something like this:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct CommunicationInfo
{
public int internalMsg;
public string srcModuleName;
public string data;
};
....
[DllImport("user32")]
public static extern IntPtr SendMessage(
IntPtr hWnd,
uint Msg,
[MarshalAs(UnmanagedType.LPWStr)] string wParam,
ref CommunicationInfo communicationInfo
);
....
CommunicationInfo communicationInfo = new CommunicationInfo
{
internalMsg = 0x0401,
srcModuleName = "ModuleName",
data = "test test"
};
SendMessage(hNppWnd, 0x111, "NppExec.dll", ref communicationInfo);

NETCF - Always on top form but SIP stays on top problem

how can I get rid of that SIP button in there? My form was supposed to be always on top, set via API SetWindowsPOS but that button still sits on top? Obviously, am not using any InputPanel as you can see there...
I am presuming the OS is Windows Mobile 6.5? Below is the code I use to disable the SIP button in 6.5:
public static class SoftInputPanel
{
const uint SIPF_OFF = 0x0;
const uint SIPF_ON = 0x1;
[DllImport("aygshell.dll")]
static extern uint SHFullScreen(IntPtr hwndRequester, uint dwState);
private const uint SHFS_HIDESIPBUTTON = 0x0008;
private const uint SHFS_SHOWSIPBUTTON = 0x0004;
[DllImport("coredll.dll")]
private extern static void SipShowIM(uint dwFlag);
public static void ShowSIP(bool isShow, Control control)
{
SHFullScreen(control.Handle, isShow ? SHFS_SHOWSIPBUTTON : SHFS_HIDESIPBUTTON);
SipShowIM(isShow ? SIPF_ON : SIPF_OFF);
}
}

screen tinter

with the help of the stackoverflow community i have designed an app that colors the screen, and makes it look like you are wearing different color glasses.
i would also like to add the functionality of instead of coloring the whole screen, ONLY coloring the background of a document exactly like this program does:
http://www.thomson-software-solutions.com/html/screen_tinter.html
anyone have any clue how to do this in vb.net?
That's a pretty simple trick, it just replaces the system color that's used for window backgrounds. You'd change it by P/Invoking the SetSysColor() API function. Here's a sample Windows Forms app that demonstrates the technique. Start a new WF app and drop a button on the form. Then paste this code:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
int oldcolor;
public Form1() {
InitializeComponent();
oldcolor = GetSysColor(COLOR_WINDOW);
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
this.button1.Click += new EventHandler(button1_Click);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
int element = COLOR_WINDOW;
SetSysColors(1, ref element, ref oldcolor);
}
private int Color2COLORREF(Color color) {
return color.R | (color.G << 8) | (color.B << 0x10);
}
private void button1_Click(object sender, EventArgs e) {
int element = COLOR_WINDOW;
int colorref = Color2COLORREF(Color.NavajoWhite);
SetSysColors(1, ref element, ref colorref);
}
private const int COLOR_WINDOW = 5;
[DllImport("user32.dll")]
private static extern bool SetSysColors(int one, ref int element, ref int color);
[DllImport("user32.dll")]
private static extern int GetSysColor(int element);
}
}
Offtopic a bit, but you can change Word to default to "White on Blue". Blue background, white text.

C# Custom PrintDialog PInvoke DevMode Question

According to the following page:
http://msdn.microsoft.com/en-us/library/ms646964(VS.85).aspx
underneath the first graphic, "If the user clicks the OK button, PrintDlg returns TRUE and the PRINTDLG structure to return informmation about the user's selection".
In this case, my custom print dialog is nearly working, but I'm trying to extract the information about printer name, orientation, etc... My understanding is that in order to retrieve the printer name, I need to examine the hDevMode value from the PRINTDLG structure to see the printer name. Is there a function that will allow me to extract that info?
My code is like (where pdlg is my defined instance of the PRINTDLG structure):
bool f = false;
try
{
f = PrintDlg(ref pdlg);
DEVMODE dm = pdlg.hDevMode;
int k = 0;
} catch (Exception ex)
{
// hopefully it doesn't fail
}
If someone has any pearlsof wisdom out there, I would sure appreciate any tips.
The following shows how to extract the printer name and driver. The key is to do a GlobalLock on hDevNames, Marshal.PtrToStructure it into the CLR version of the struct, and then access its content. Remember to GlobalUnlock when done.
You could do something similar with hDevMode, which will get you information about the printer metrics and setup. You can find a C# declaration of the DEVMODE struct here.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication3 {
class Program {
// Win32 struct declarations
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
[System.Runtime.InteropServices.ComVisible(false)]
internal class PRINTDLG {
public Int32 lStructSize;
public IntPtr hwndOwner;
public IntPtr hDevMode;
public IntPtr hDevNames;
public IntPtr hDC = IntPtr.Zero;
public Int32 Flags;
public Int16 FromPage = 0;
public Int16 ToPage = 0;
public Int16 MinPage = 0;
public Int16 MaxPage = 0;
public Int16 Copies = 0;
public IntPtr hInstance = IntPtr.Zero;
public IntPtr lCustData = IntPtr.Zero;
public IntPtr lpfnPrintHook;
public IntPtr lpfnSetupHook = IntPtr.Zero;
public IntPtr lpPrintTemplateName = IntPtr.Zero;
public IntPtr lpSetupTemplateName = IntPtr.Zero;
public IntPtr hPrintTemplate = IntPtr.Zero;
public IntPtr hSetupTemplate = IntPtr.Zero;
}
[StructLayout(LayoutKind.Sequential)]
public class DEVNAMES {
public short wDriverOffset;
public short wDeviceOffset;
public short wOutputOffset;
public short wDefault;
}
// import PrintDlg, GlobalLock and GlobalUnlock
[DllImport("comdlg32.dll", CharSet = CharSet.Auto)]
private static extern bool PrintDlg([In, Out] PRINTDLG lppd);
[DllImport("kernel32.dll")]
private static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
private static extern bool GlobalUnlock(IntPtr hMem);
static void Main(string[] args) {
// show the printer dialog box
PRINTDLG pd = new PRINTDLG();
pd.lStructSize = Marshal.SizeOf(pd);
PrintDlg(pd);
// here's the meat -- extract the printer information
// out of pd.hDevNames...
DEVNAMES devNames = new DEVNAMES();
// lock hDevNames into memory and get a pointer to it
IntPtr pDevNames = GlobalLock(pd.hDevNames);
// marshal into a DEVNAME struct
Marshal.PtrToStructure(pDevNames, devNames);
// pull out the device and driver strings; hopefully not much of
// that in DEVMODE
string sDevice = Marshal.PtrToStringUni((IntPtr) (
pDevNames.ToInt32() +
devNames.wDeviceOffset * Marshal.SystemDefaultCharSize));
string sDriver = Marshal.PtrToStringUni((IntPtr) (
pDevNames.ToInt32() +
devNames.wDriverOffset * Marshal.SystemDefaultCharSize));
string sOutput = Marshal.PtrToStringUni((IntPtr) (
pDevNames.ToInt32() +
devNames.wOutputOffset * Marshal.SystemDefaultCharSize));
// done -- release the global memory handle
GlobalUnlock(pd.hDevNames);
}
}
}