lwip_connect: invalid address" failed error? - sockets

Am trying to connect to a socket using Xamarin Binding project for RedPark's RedSocket SDK.Am getting the below error
lwip_connect: invalid address" failed at line 495 in
/Users/jeremy/Dev/Folsom/Folsom_570/RecEthernet/tcp/api/sockets.c
But I dont see any user exists with that name in my mac, Can anyone help me in understanding the root cause?
My Binding project's ApiDefinitions.cs code
namespace RedparkEthernetBinding
{
using System.Net;
using System;
using Foundation;
using RedparkEthernetBinding;
// #protocol RedSocketManagerProtocol <NSObject>
[Protocol]
[BaseType (typeof(NSObject))]
interface RedSocketManagerProtocol
{
// #required -(void)configureNetworkInterface:(NSString *)deviceIPAddress gateway:(NSString *)gateway netmask:(NSString *)netmask dns:(NSString *)dns;
[Export ("configureNetworkInterface:gateway:netmask:dns:")]
void ConfigureNetworkInterface (string deviceIPAddress, string gateway, string netmask, string dns);
// #required -(NSString *)ipAddress;
[Export ("ipAddress")]
string IpAddress { get; }
// #required -(NSString *)gatewayAddress;
[Export ("gatewayAddress")]
string GatewayAddress { get; }
// #required -(NSString *)netmask;
[Export ("netmask")]
string Netmask { get; }
// #required -(NSString *)dnsAddress;
[Export ("dnsAddress")]
string DnsAddress { get; }
// #required -(int)socket:(int)domain type:(int)type protocol:(int)protocol;
[Export ("socket:type:protocol:")]
int Socket (int domain, int type, int protocol);
// #required -(int)accept:(int)s addr:(struct sockaddr *)addr addrlen:(socklen_t *)addrlen;
//[Abstract]
[Export ("accept:addr:addrlen:")]
unsafe int Accept (int s, IntPtr addr, uint addrlen);
// #required -(void)acceptAsync:(int)listenSocket addr:(struct sockaddr *)addr addrlen:(socklen_t *)addrlen;
[Export ("acceptAsync:addr:addrlen:")]
unsafe void AcceptAsync (int listenSocket,IntPtr addr, uint addrlen);
// #required -(int)bind:(int)s name:(const struct sockaddr *)name nameline:(socklen_t)namelen;
[Export ("bind:name:nameline:")]
unsafe int Bind (int s,IntPtr name, uint namelen);
// #required -(int)shutdown:(int)s how:(int)how;
[Export ("shutdown:how:")]
int Shutdown (int s, int how);
// #required -(int)getpeername:(int)s name:(struct sockaddr *)name namelen:(socklen_t *)namelen;
[Export ("getpeername:name:namelen:")]
unsafe int Getpeername (int s,IntPtr name, uint namelen);
// #required -(int)getsockname:(int)s name:(struct sockaddr *)name namelen:(socklen_t *)namelen;
[Export ("getsockname:name:namelen:")]
unsafe int Getsockname (int s,IntPtr name, uint namelen);
// #required -(int)getsockopt:(int)s level:(int)level optname:(int)optname optval:(void *)optval optlen:(socklen_t *)optlen;
[Export ("getsockopt:level:optname:optval:optlen:")]
unsafe int Getsockopt (int s, int level, int optname, IntPtr optval, uint optlen);
// #required -(int)setsockopt:(int)s level:(int)level optname:(int)optname optval:(const void *)optval opelen:(socklen_t)optlen;
[Export ("setsockopt:level:optname:optval:opelen:")]
unsafe int Setsockopt (int s, int level, int optname, IntPtr optval, uint optlen);
// #required -(int)close:(int)s;
[Export ("close:")]
int Close (int s);
// #required -(int)connect:(int)s sockaddr:(const struct sockaddr *)name namelen:(socklen_t)namelen;
[Export ("connect:sockaddr:namelen:")]
unsafe int Connect (int s, IntPtr name, uint namelen);
// #required -(int)listen:(int)s backlog:(int)backlog;
[Export ("listen:backlog:")]
int Listen (int s, int backlog);
// #required -(int)recv:(int)s mem:(void *)mem len:(size_t)len flags:(int)flags;
[Export ("recv:mem:len:flags:")]
unsafe int Recv (int s, IntPtr mem, nuint len, int flags);
// #required -(int)read:(int)s mem:(void *)mem len:(size_t)len;
[Export ("read:mem:len:")]
unsafe int Read (int s, IntPtr mem, nuint len);
// #required -(int)recvfrom:(int)s mem:(void *)mem len:(size_t)len flags:(int)flags from:(struct sockaddr *)from fromlen:(socklen_t *)fromlen;
[Export ("recvfrom:mem:len:flags:from:fromlen:")]
unsafe int Recvfrom (int s, IntPtr mem, nuint len, int flags,IntPtr from, uint fromlen);
// #required -(void)recvAsync:(int)sock buffer:(void *)buffer length:(size_t)length;
[Export ("recvAsync:buffer:length:")]
unsafe void RecvAsync (int sock, IntPtr buffer, nuint length);
// #required -(int)send:(int)s dataptr:(const void *)dataptr size:(size_t)size flags:(int)flags;
[Export ("send:dataptr:size:flags:")]
unsafe int Send (int s, IntPtr dataptr, nuint size, int flags);
// #required -(int)sendto:(int)s dataprt:(const void *)dataptr size:(size_t)size flags:(int)flags to:(const struct sockaddr *)to tolen:(socklen_t)tolen;
[Export ("sendto:dataprt:size:flags:to:tolen:")]
unsafe int Sendto (int s, IntPtr dataptr, nuint size, int flags, IntPtr to, uint tolen);
// #required -(int)write:(int)s dataptr:(const void *)dataptr size:(size_t)size;
[Export ("write:dataptr:size:")]
unsafe int Write (int s, IntPtr dataptr, nuint size);
// // #required -(int)select:(int)maxfdp1 readset:(fd_set *)readset writeset:(fd_set *)writeset exceptset:(fd_set *)exceptset timeout:(struct timeval *)timeout;
//
// [Export ("select:readset:writeset:exceptset:timeout:")]
// unsafe int Select (int maxfdp1, fd_set readset, fd_set writeset, fd_set exceptset, timeval timeout);
// #required -(int)ioctl:(int)s cmd:(long)cmd argp:(void *)argp;
[Export ("ioctl:cmd:argp:")]
unsafe int Ioctl (int s, nint cmd, IntPtr argp);
// #required -(int)fcntl:(int)s cmd:(int)cmd val:(int)val;
[Export ("fcntl:cmd:val:")]
int Fcntl (int s, int cmd, int val);
// #required -(int)abort_recv:(int)s;
[Export ("abort_recv:")]
int Abort_recv (int s);
// #required -(int)abort_all:(int)s;
[Export ("abort_all:")]
int Abort_all (int s);
[Export("Delegate"),NullAllowed]
NSObject WeakDelegate { get; set;}
[Wrap("WeakDelegate")]
RedSocketManager Delegate{ get; set;}
// #required -(BOOL)isCableConnected;
[Export ("isCableConnected")]
bool IsCableConnected { get; }
// #required -(NSString *)getAccessoryFirmwareVersion;
[Export ("getAccessoryFirmwareVersion")]
string AccessoryFirmwareVersion { get; }
// #required -(int)getRxCounter;
[Export ("getRxCounter")]
int RxCounter { get; }
// #required -(void)resetRxCounter;
[Export ("resetRxCounter")]
void ResetRxCounter ();
}
// #protocol RedSocketManagerDelegate <NSObject>
[Protocol, Model]
[BaseType (typeof(NSObject))]
interface RedSocketManagerDelegate
{
// #required -(void)cableConnected:(NSString *)protocol;
[Abstract]
[Export ("cableConnected:")]
void CableConnected (string protocol);
[Abstract]
// #required -(void)cableDisconnected;
[Export ("cableDisconnected")]
void CableDisconnected ();
// #optional -(void)didAssignIpAddress:(NSString *)deviceAddress gateway:(NSString *)gateway netmask:(NSString *)netmask;
[Export ("didAssignIpAddress:gateway:netmask:")]
void DidAssignIpAddress (string deviceAddress, string gateway, string netmask);
// #optional -(void)didSocketRecv:(int)socket buffer:(void *)buffer bytesRead:(int)bytesRead;
[Export ("didSocketRecv:buffer:bytesRead:")]
unsafe void DidSocketRecv (int socket, IntPtr buffer, int bytesRead);
// #optional -(void)didSocketAccept:(int)newSocket addr:(struct sockaddr *)addr addrlen:(socklen_t *)addrlen;
[Export ("didSocketAccept:addr:addrlen:")]
unsafe void DidSocketAccept (int newSocket,IntPtr addr, uint addrlen);
}
// [Protocol, Model]
// [BaseType (typeof(NSObject))]
// interface RedSocketManagerProtocol
// {}
//
// #interface RedSocketManager : NSObject
[BaseType (typeof(NSObject))]
interface RedSocketManager
{
[Static]
// +(id<RedSocketManagerProtocol>)sharedInstance;
[Export ("sharedInstance")]
RedSocketManagerProtocol SharedInstance { get; }
}
}
structures & Enums,
public struct hostent
{
}
static class CFunctions
{
// extern struct hostent * redsocket_gethostbyname (const char * name);
[DllImport ("__Internal")]
static extern unsafe hostent* redsocket_gethostbyname (sbyte* name);
// extern UInt32 RedEthernet_calcCRC32 (UInt32 * buffer, unsigned int numWords);
[DllImport ("__Internal")]
static extern unsafe uint RedEthernet_calcCRC32 (uint* buffer, uint numWords);
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct sockaddr {
/// u_short->unsigned short
public ushort sa_family;
/// char[14]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst=14)]
public string sa_data;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct fd_set {
/// u_int->unsigned int
public uint fd_count;
/// SOCKET[64]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=64, ArraySubType=UnmanagedType.U4)]
public uint[] fd_array;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct timeval {
/// int
public int tv_sec;
/// int
public int tv_usec;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct sockaddr_in {
/// short
public short sin_family;
/// u_short->unsigned short
public ushort sin_port;
/// in_addr
public in_addr sin_addr;
/// char[8]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst=8)]
public string sin_zero;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct in_addr {
public uint s_addr;
}
Thanks,
Ravi

This error is coming because of the sockaddr structure pointer is not holding the ip address details in connect method.

Related

I am struggling to get started with a Stack and Queue and was wondering if anyone could help based on these Classes?

#include <iostream>;
using namespace std;
class Node
{
public:
Node(int value, Node* nextptr = NULL, Node* prevptr = NULL, int currentpriority = 0);
int getVal(void);
Node* getNext(void);
Node* getPrev(void);
void setVal(int value);
void setPrev(Node* prevptr);
void setNext(Node* nextptr);
int getPriority(void);
void setPriority(int priority);
private:
Node* next;
Node* prev;
int priority;
int value;
};
class Stack
{
public:
Stack(void);
~Stack(void);
void Push(int value);
Node* NodePop(void);
int Pop(void);
private:
Node* top;
};
class Queue
{
public:
Queue(void);
~Queue(void);
void Enqueue(int i, int priority = 0);
int Dequeue(void);
protected:
Node* back;
Node* front;
private:
virtual Node* NodeDequeue(void);
};
So these are the classes we have been given I am struggling to just get the ball rolling in a sense. I have done a simple stack last year with pointers and Templates but I am having trouble figuring out where to start with the added feature of the get and set functions. I am only allowed to use iostream also as a heads up.

UnityKinectDepthExplorer - how can I use this example with Kinect v1?

I'm interested in displaying Depth data in Unity with Kinect for Windows. Unfortunately, I have Kinect v1.8 and I know this has been easily done with Kinect V2 (Kinect Fusion).
I found this example: https://github.com/rickbarraza/UnityKinectDepthExplorer
I installed all the necessary components and now getting DLLNOTFOUNDEXCEPTION:
Failed to load 'Assets/Plugins/x86_64/KinectUnityAddin.dll'
Concerning all the posts and forums I've been reading so far, It's because I'm using SDK 1.8 (instead of SDK 2.0).
Is there any way how I can use this example with Kinect v1.8? I tried to contact the author as well but he haven't responded back.
Thanks a lot!
I had the same Problem in an University Project maby its not to late for you.
This is an Kinect v1.8 Wrapper i made to read the kinect depth image only. I used the Kinect with MS-SDK from the Unity-Assetstore as an orientation, you dont have to download it. All you need is this Code.
Use The Init() Method to start and the GetDepthArray() Function to get the Deapth Frame.
hope it works for you !
You have to install the Kinect 1.8 sdk or have at least the Kinect10.dll from the 1.8 sdk in your Windows/System32 folder
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.Runtime.CompilerServices;
public class OwnKinectWrapper : MonoBehaviour {
DepthBuffer db;
#region Nui Variables/Structs/Intefraces
IntPtr streamReferenz;
public struct NuiImageViewArea
{
public int eDigitalZoom;
public int lCenterX;
public int lCenterY;
}
public struct NuiSurfaceDesc
{
uint width;
uint height;
}
public struct DepthBuffer
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 640 * 480, ArraySubType = UnmanagedType.U2)]
public ushort[] pixels;
}
public struct NuiLockedRect
{
public int pitch;
public int size;
public IntPtr pBits;
}
public struct NuiImageFrame
{
public Int64 liTimeStamp;
public uint dwFrameNumber;
public int eImageType;
public int eResolution;
public IntPtr pFrameTexture;
public uint dwFrameFlags_NotUsed;
public NuiImageViewArea ViewArea_NotUsed;
}
[Guid("13ea17f5-ff2e-4670-9ee5-1297a6e880d1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport()]
public interface INuiFrameTexture
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
[PreserveSig]
int BufferLen();
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
[PreserveSig]
int Pitch();
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
[PreserveSig]
int LockRect(uint Level, ref NuiLockedRect pLockedRect, IntPtr pRect, uint Flags);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
[PreserveSig]
int GetLevelDesc(uint Level, ref NuiSurfaceDesc pDesc);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
[PreserveSig]
int UnlockRect(uint Level);
}
#endregion
// Use this for initialization
public int Init() {
int init = 0;
try {
init = NuiInitialize(0x00000020);
Debug.Log("init : " + init);
streamReferenz = IntPtr.Zero;
NuiImageStreamOpen(4, 2, 0, 2, IntPtr.Zero, ref streamReferenz);
}
catch (DllNotFoundException e)
{
string message = "Please check the Kinect SDK installation.";
Debug.LogError(message);
Debug.LogError(e.ToString());
return -1;
}
catch (Exception e)
{
Debug.LogError(e.ToString());
return -1;
}
return init;
}
// Update is called once per frame
void Update()
{
IntPtr imageStreamFrameReferenz = IntPtr.Zero;
int test = NuiImageStreamGetNextFrame(streamReferenz, 0, ref imageStreamFrameReferenz);
if (test == 0) {
NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageStreamFrameReferenz, typeof(NuiImageFrame));
INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);
NuiLockedRect lockedRectPtr = new NuiLockedRect();
IntPtr r = IntPtr.Zero;
frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
db = (DepthBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(DepthBuffer));
frameTexture.UnlockRect(0);
NuiImageStreamReleaseFrame(streamReferenz, imageStreamFrameReferenz);
}
}
void OnDisable() {
NuiShutdown();
}
public ushort[] GetDepthArray(){
return db.pixels;
}
[DllImportAttribute(#"Kinect10.dll",EntryPoint="NuiInitialize")]
public static extern int NuiInitialize (uint dwFlags);
[DllImportAttribute(#"Kinect10.dll", EntryPoint = "NuiImageStreamOpen")]
public static extern int NuiImageStreamOpen(int enumImageType,int enumImgageResolution, uint image_Flags, uint frameBufferLimit, IntPtr nextFrameEvent, ref IntPtr streamHandle );
[DllImportAttribute(#"Kinect10.dll", EntryPoint = "NuiImageStreamGetNextFrame")]
public static extern int NuiImageStreamGetNextFrame(IntPtr streamReferenz, uint dwMillisecondsToWait, ref IntPtr ImageFrameReferenz);
[DllImportAttribute(#"Kinect10.dll", EntryPoint = "NuiImageStreamReleaseFrame")]
public static extern int NuiImageStreamReleaseFrame(IntPtr phStreamHandle, IntPtr ppcImageFrame);
[DllImportAttribute(#"Kinect10.dll",EntryPoint="NuiDepthPixelToDepth")]
public static extern ushort NuiDepthPixelToDepth (ushort depthPixel);
[DllImportAttribute(#"Kinect10.dll", EntryPoint = "NuiShutdown")]
public static extern void NuiShutdown();
}
if something is not working or you need help feel free to write some lines

Detecting text changes in Word 2016 from VSTO add-in

This question is very closely related to How to get the “KeyPress” event from a Word 2010 Addin (developed in C#)? (and in fact includes the sample code from the answer to that question), but this is specifically about developing in Visual Studio (Professional) 2015 for Word 2016 running in Windows 10.
I’m trying to detect when text changes in a Word document from a VSTO add-in. I understand from
How to get the “KeyPress” event from a Word 2010 Addin (developed in C#)? (Nov 14, 2011)
Capturing keydown event of MS Word using C# (Oct 21, 2012)
How to raise an event on MS word Keypress (Oct 24, 2012)
How to trap keypress event in MSword using VSTO? (Nov 5, 2012)
that there’s no event-driven way to do this. Word simply does not send events when text changes.
I’ve seen two workarounds discussed:
Use the WindowSelectionChange event. Unfortunately, this event appears to be sent when the selection is changed by pressing arrow keys, using the mouse, performing undo or redo, and probably other actions, but not when typing or deleting.
Use a low-level keydown event hook. This has been discussed in several of those StackOverflow questions, and was also called a “widely spread technique” in a thread on a Visual Studio forum in February 2014.
I’m trying to use the code in the answer to How to get the “KeyPress” event from a Word 2010 Addin (developed in C#)?, and it seems to observe every keydown event except those sent to Word 2016.
Here’s the code I’m using, for ease of reference.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace KeydownWordAddIn
{
public partial class ThisAddIn
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static IntPtr hookId = IntPtr.Zero;
private delegate IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam);
private static HookProcedure procedure = HookCallback;
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr SetHook(HookProcedure procedure)
{
using (Process process = Process.GetCurrentProcess())
using (ProcessModule module = process.MainModule)
return SetWindowsHookEx(WH_KEYBOARD_LL, procedure, GetModuleHandle(module.ModuleName), 0);
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int pointerCode = Marshal.ReadInt32(lParam);
string pressedKey = ((Keys)pointerCode).ToString();
// Do some sort of processing on key press.
var thread = new Thread(() => {
Debug.WriteLine(pressedKey);
});
thread.Start();
}
return CallNextHookEx(hookId, nCode, wParam, lParam);
}
private void ThisAddIn_Startup(object sender, EventArgs e)
{
hookId = SetHook(procedure);
}
private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
UnhookWindowsHookEx(hookId);
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
When I run Word 2016 with this add-in, I see keydown events sent to the Edge browser and even Visual Studio, but not to Word itself.
Are keydown hooks somehow prevented in Word 2016, or am I doing something wrong?
Everthing should work fine if you don't use a low-level hook in your VSTO add-in.
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetCurrentThreadId();
const int WH_KEYBOARD = 2;
private static IntPtr SetHook(HookProcedure procedure)
{
var threadId = (uint)SafeNativeMethods.GetCurrentThreadId();
return SetWindowsHookEx(WH_KEYBOARD, procedure, IntPtr.Zero, threadId);
}
Please note that you probably also need to create a hook to intercept mouse messages as it is possible to modify the text of a document solely by mouse interactions (e.g. copy and paste via Ribbon or context menu).
VSTO Sample
Here is a complete working VSTO sample including keyboard and mouse hooks:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
namespace SampleAddinWithKeyboardHook
{
public partial class ThisAddIn
{
// NOTE: We need a backing field to prevent the delegate being garbage collected
private SafeNativeMethods.HookProc _mouseProc;
private SafeNativeMethods.HookProc _keyboardProc;
private IntPtr _hookIdMouse;
private IntPtr _hookIdKeyboard;
private void ThisAddIn_Startup(object sender, EventArgs e)
{
_mouseProc = MouseHookCallback;
_keyboardProc = KeyboardHookCallback;
SetWindowsHooks();
}
private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
UnhookWindowsHooks();
}
private void SetWindowsHooks()
{
uint threadId = (uint)SafeNativeMethods.GetCurrentThreadId();
_hookIdMouse =
SafeNativeMethods.SetWindowsHookEx(
(int)SafeNativeMethods.HookType.WH_MOUSE,
_mouseProc,
IntPtr.Zero,
threadId);
_hookIdKeyboard =
SafeNativeMethods.SetWindowsHookEx(
(int)SafeNativeMethods.HookType.WH_KEYBOARD,
_keyboardProc,
IntPtr.Zero,
threadId);
}
private void UnhookWindowsHooks()
{
SafeNativeMethods.UnhookWindowsHookEx(_hookIdKeyboard);
SafeNativeMethods.UnhookWindowsHookEx(_hookIdMouse);
}
private IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var mouseHookStruct =
(SafeNativeMethods.MouseHookStructEx)
Marshal.PtrToStructure(lParam, typeof(SafeNativeMethods.MouseHookStructEx));
// handle mouse message here
var message = (SafeNativeMethods.WindowMessages)wParam;
Debug.WriteLine(
"{0} event detected at position {1} - {2}",
message,
mouseHookStruct.pt.X,
mouseHookStruct.pt.Y);
}
return SafeNativeMethods.CallNextHookEx(
_hookIdKeyboard,
nCode,
wParam,
lParam);
}
private IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
// handle key message here
Debug.WriteLine("Key event detected.");
}
return SafeNativeMethods.CallNextHookEx(
_hookIdKeyboard,
nCode,
wParam,
lParam);
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support.
/// </summary>
private void InternalStartup()
{
Startup += ThisAddIn_Startup;
Shutdown += ThisAddIn_Shutdown;
}
#endregion
}
internal static class SafeNativeMethods
{
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
public enum HookType
{
WH_KEYBOARD = 2,
WH_MOUSE = 7
}
public enum WindowMessages : uint
{
WM_KEYDOWN = 0x0100,
WM_KEYFIRST = 0x0100,
WM_KEYLAST = 0x0108,
WM_KEYUP = 0x0101,
WM_LBUTTONDBLCLK = 0x0203,
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MBUTTONDBLCLK = 0x0209,
WM_MBUTTONDOWN = 0x0207,
WM_MBUTTONUP = 0x0208,
WM_MOUSEACTIVATE = 0x0021,
WM_MOUSEFIRST = 0x0200,
WM_MOUSEHOVER = 0x02A1,
WM_MOUSELAST = 0x020D,
WM_MOUSELEAVE = 0x02A3,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_MOUSEHWHEEL = 0x020E,
WM_RBUTTONDBLCLK = 0x0206,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_SYSDEADCHAR = 0x0107,
WM_SYSKEYDOWN = 0x0104,
WM_SYSKEYUP = 0x0105
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(
IntPtr hhk,
int nCode,
IntPtr wParam,
IntPtr lParam);
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetCurrentThreadId();
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
public static implicit operator System.Drawing.Point(Point p)
{
return new System.Drawing.Point(p.X, p.Y);
}
public static implicit operator Point(System.Drawing.Point p)
{
return new Point(p.X, p.Y);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct MouseHookStructEx
{
public Point pt;
public IntPtr hwnd;
public uint wHitTestCode;
public IntPtr dwExtraInfo;
public int MouseData;
}
}
}
VBE Add-in Sample
And here is a working sample for the VBA editor (VBE add-in):
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Extensibility;
namespace VbeAddin
{
[ComVisible(true)]
[ProgId("VbeAddin.Connect")]
[Guid("95840C70-5A1A-4EDB-B436-40E8BF030469")]
public class Connect : StandardOleMarshalObject, IDTExtensibility2
{
// NOTE: We need a backing field to prevent the delegate being garbage collected
private SafeNativeMethods.HookProc _mouseProc;
private SafeNativeMethods.HookProc _keyboardProc;
private IntPtr _hookIdMouse;
private IntPtr _hookIdKeyboard;
#region IDTExtensibility2 Members
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_mouseProc = MouseHookCallback;
_keyboardProc = KeyboardHookCallback;
SetWindowsHooks();
}
public void OnDisconnection(ext_DisconnectMode removeMode, ref Array custom)
{
UnhookWindowsHooks();
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
}
public void OnBeginShutdown(ref Array custom)
{
}
#endregion
private void SetWindowsHooks()
{
uint threadId = (uint)SafeNativeMethods.GetCurrentThreadId();
_hookIdMouse =
SafeNativeMethods.SetWindowsHookEx(
(int)SafeNativeMethods.HookType.WH_MOUSE,
_mouseProc,
IntPtr.Zero,
threadId);
_hookIdKeyboard =
SafeNativeMethods.SetWindowsHookEx(
(int)SafeNativeMethods.HookType.WH_KEYBOARD,
_keyboardProc,
IntPtr.Zero,
threadId);
}
private void UnhookWindowsHooks()
{
SafeNativeMethods.UnhookWindowsHookEx(_hookIdKeyboard);
SafeNativeMethods.UnhookWindowsHookEx(_hookIdMouse);
}
private IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var mouseHookStruct =
(SafeNativeMethods.MouseHookStructEx)
Marshal.PtrToStructure(
lParam,
typeof(SafeNativeMethods.MouseHookStructEx));
// handle mouse message here
var message = (SafeNativeMethods.WindowMessages)wParam;
Debug.WriteLine(
"{0} event detected at position {1} - {2}",
message,
mouseHookStruct.pt.X,
mouseHookStruct.pt.Y);
}
return SafeNativeMethods.CallNextHookEx(
_hookIdKeyboard,
nCode,
wParam,
lParam);
}
private IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
// handle key message here
Debug.WriteLine("Key event detected.");
}
return SafeNativeMethods.CallNextHookEx(
_hookIdKeyboard,
nCode,
wParam,
lParam);
}
}
internal static class SafeNativeMethods
{
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
public enum HookType
{
WH_KEYBOARD = 2,
WH_MOUSE = 7
}
public enum WindowMessages : uint
{
WM_KEYDOWN = 0x0100,
WM_KEYFIRST = 0x0100,
WM_KEYLAST = 0x0108,
WM_KEYUP = 0x0101,
WM_LBUTTONDBLCLK = 0x0203,
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MBUTTONDBLCLK = 0x0209,
WM_MBUTTONDOWN = 0x0207,
WM_MBUTTONUP = 0x0208,
WM_MOUSEACTIVATE = 0x0021,
WM_MOUSEFIRST = 0x0200,
WM_MOUSEHOVER = 0x02A1,
WM_MOUSELAST = 0x020D,
WM_MOUSELEAVE = 0x02A3,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_MOUSEHWHEEL = 0x020E,
WM_RBUTTONDBLCLK = 0x0206,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_SYSDEADCHAR = 0x0107,
WM_SYSKEYDOWN = 0x0104,
WM_SYSKEYUP = 0x0105
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(
IntPtr hhk,
int nCode,
IntPtr wParam,
IntPtr lParam);
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetCurrentThreadId();
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public struct MouseHookStructEx
{
public Point pt;
public IntPtr hwnd;
public uint wHitTestCode;
public IntPtr dwExtraInfo;
public int MouseData;
}
}
}
I've experienced this same exact issue in Word 2013 and had to come up with a somewhat "creative" solution. It uses diffplex to monitor changes in the text of the active document and fires events when it changes. It's less than ideal but we do what we have to do to make things work.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using System.ComponentModel;
namespace WordUtils {
public class TextChangeDetector {
public Word.Application Application;
private BackgroundWorker bg;
public delegate void TextChangeHandler(object sender, TextChangedEventArgs e);
public event TextChangeHandler OnTextChanged;
public TextChangeDetector(Word.Application app) {
this.Application = app;
}
public void Start() {
bg = new BackgroundWorker();
bg.WorkerReportsProgress = true;
bg.WorkerSupportsCancellation = true;
bg.ProgressChanged += bg_ProgressChanged;
bg.DoWork += bg_DoWork;
bg.RunWorkerAsync(this.Application);
}
private void bg_ProgressChanged(object sender, ProgressChangedEventArgs e) {
switch (e.ProgressPercentage) {
case 50: //change
if (OnTextChanged != null) {
OnTextChanged(this, new TextChangedEventArgs((char)e.UserState));
}
break;
}
}
private void bg_DoWork(object sender, DoWorkEventArgs e) {
Word.Application wordApp = e.Argument as Word.Application;
BackgroundWorker bg = sender as BackgroundWorker;
string lastPage = string.Empty;
while (true) {
try {
if (Application.Documents.Count > 0) {
if (Application.ActiveDocument.Words.Count > 0) {
var currentPage = Application.ActiveDocument.Bookmarks["\\Page"].Range.Text;
if (currentPage != null && currentPage != lastPage) {
var differ = new DiffPlex.Differ();
var builder = new DiffPlex.DiffBuilder.InlineDiffBuilder(differ);
var difference = builder.BuildDiffModel(lastPage, currentPage);
var change = from d in difference.Lines where d.Type != DiffPlex.DiffBuilder.Model.ChangeType.Unchanged select d;
if (change.Any()) {
bg.ReportProgress(50, change.Last().Text.Last());
}
lastPage = currentPage;
}
}
}
} catch (Exception) {
}
if (bg.CancellationPending) {
break;
}
System.Threading.Thread.Sleep(100);
}
}
public void Stop() {
if (bg != null && !bg.CancellationPending) {
bg.CancelAsync();
}
}
}
public class TextChangedEventArgs : EventArgs {
public char Letter;
public TextChangedEventArgs(char letter) {
this.Letter = letter;
}
}
}
Usage:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using WordUtils;
namespace WordAddIn1 {
public partial class ThisAddIn {
TextChangeDetector detector;
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
detector = new TextChangeDetector(Application);
detector.OnTextChanged += detector_OnTextChanged;
detector.Start();
}
void detector_OnTextChanged(object sender, TextChangedEventArgs e) {
Console.WriteLine(e.Letter);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e) {
detector.Stop();
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup() {
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}

configuring serial port from kernel space

How to configure the serial port in kernel module. I am doing this in init module function. same configuration is working in userpsace. I am using the below code to configure the serial port.
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(KERNEL_DS);
fp = filp_open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
tty = (struct tty_struct *)fp->private_data;
setting the required configuration(tty->termios)
set_fs(oldfs);
Almost all functions of serial port operations are implemented by drivers/tty/serial/serial_core.c. If you want to do it differently then provide your own functions for uart_ops from include/linux/serial_core.h
struct uart_ops {
unsigned int (*tx_empty)(struct uart_port *);
void (*set_mctrl)(struct uart_port *, unsigned int mctrl);
unsigned int (*get_mctrl)(struct uart_port *);
void (*stop_tx)(struct uart_port *);
void (*start_tx)(struct uart_port *);
void (*send_xchar)(struct uart_port *, char ch);
void (*stop_rx)(struct uart_port *);
void (*enable_ms)(struct uart_port *);
void (*break_ctl)(struct uart_port *, int ctl);
int (*startup)(struct uart_port *);
void (*shutdown)(struct uart_port *);
void (*flush_buffer)(struct uart_port *);
void (*set_termios)(struct uart_port *, struct ktermios *new,
struct ktermios *old);
void (*set_ldisc)(struct uart_port *, int new);
void (*pm)(struct uart_port *, unsigned int state,
unsigned int oldstate);
int (*set_wake)(struct uart_port *, unsigned int state);
const char * (*type)(struct uart_port *);
void (*release_port)(struct uart_port *);
int (*request_port)(struct uart_port *);
void (*config_port)(struct uart_port *, int);
int (*verify_port)(struct uart_port *, struct serial_struct *);
int (*ioctl)(struct uart_port *, unsigned int, unsigned long);
#ifdef CONFIG_CONSOLE_POLL
void (*poll_put_char)(struct uart_port *, unsigned char);
int (*poll_get_char)(struct uart_port *);
#endif
};
In your case, you need to implement set_termios function. Also, look at uart_get_divisor() to know how to set baud rate.

Defining an interface to pass byte[] and byte* between C# and C++?

Given the following test interface (C#):
public interface ITest
{
UInt32 Simple(UInt32 someArg);
byte* Read(UInt32 count);
void Write(byte* buf, UInt32 count);
byte[] ReadArray(UInt32 count);
void WriteArray(byte[] buf);
}
Can I implement this interface in c++/cli? I've tried a million different ways; currently I have this:
typedef unsigned __int32 uint32;
typedef unsigned char byte;
public ref class CTest : public ITest
{
virtual uint32 Simple(uint32 someArg);
virtual byte * Read(uint32 count);
virtual void Write(byte * buf, uint32 count);
virtual System::Array<byte>^ ReadArray(uint32 count);
virtual void WriteArray(System::Array<byte>^ buf);
}
The VC2010 compiler complains bitterly about all of the methods except CTest::Simple, claiming I haven't implemented the interface.
Can someone show me the c++/cli magic to implement an interface that passes either byte* or byte[] (preferably both)? I'm at my wit's end...
The following compiles:
public ref class CTest : public ITest
{
public:
virtual unsigned Simple(unsigned someArg) { return 0u; }
virtual unsigned char* Read(unsigned count) { return nullptr; }
virtual void Write(unsigned char* buf, unsigned count) { }
virtual array<unsigned char>^ ReadArray(unsigned count) { return nullptr; }
virtual void WriteArray(array<unsigned char>^ buf) { }
};
Your main problem is the fact that your CTest member functions are private and thus ineligible to implement the interface. The other problems are mostly related to using System::Array<> rather than array<> (which is short for cli::array<>).