I have a EOS 1100D and use edsdk to taking picture by my camera from my computer.
in Manual mode I should can chand every properties, and in EOS Utility that is released by Canon, in Manual mode, user can switch between AF and MF. So, there should be a property!
What I found in EDSDK.cs is:
/*---------------------------------------------
Focus Info
----------------------------------------------*/
[StructLayout(LayoutKind.Sequential)]
public struct EdsFocusPoint
{
public uint valid;
public uint selected;
public uint justFocus;
public EdsRect rect;
public uint reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct EdsFocusInfo
{
public EdsRect imageRect;
public uint pointNumber;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public EdsFocusPoint[] focusPoint;
public uint executeMode;
}
How can I set this executeMode?
In fact, I should set in to what for AF and what for MF?
you can do this with EdsSendCommand where inCommand is kEdsCameraCommand_PressShutterButton and inParam is one of the kEdsCameraCommand_ShutterButton_ values.
With it you can control the shutterbutton (i.e. press it remotely).
To take a photo without AF, you simply use the value CameraCommand_ShutterButton_Completely_NonAF
But don't forget to set it back to CameraCommand_ShutterButton_OFF after you have taken the photo!
In the EDSDK documentation you can find details on page 42 and 43.
Related
None of the methods in Unsafe class do work when trying to read a structure from a pointer.
They all display Unable to read memory in the debugger:
var info1 = Marshal.PtrToStructure<DbgHelp.SYMBOL_INFO_V>(pSymInfo); // works
var info2 = Unsafe.AsRef<DbgHelp.SYMBOL_INFO_V>((void*)pSymInfo); // unable to read memory
var info3 = Unsafe.Read<DbgHelp.SYMBOL_INFO_V>((void*)pSymInfo); // unable to read memory
var info4 = Unsafe.ReadUnaligned<DbgHelp.SYMBOL_INFO_V>((void*)pSymInfo); // unable to read memory
Is it possible to read a structure with Unsafe class just as Marshal.PtrToStructure does?
About:
pSymInfo is an IntPtr.
SYMBOL_INFO_V:
public struct SYMBOL_INFO_V
{
public uint SizeOfStruct;
public uint TypeIndex;
public ulong Reserved0;
private ulong Reserved1;
public uint Index;
public uint Size;
public ulong ModBase;
public SYMFLAG Flags; // enum, uint
public ulong Value;
public ulong Address;
public uint Register;
public uint Scope;
public uint Tag;
public uint NameLen;
public uint MaxNameLen;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string Name;
}
The Unsafe class has no knowledge of P/Invoke rules, only the Marshal class does.
For example, if you take this little program:
unsafe internal class Program
{
static void Main()
{
var info = new MyStruct();
Console.WriteLine(Marshal.SizeOf<MyStruct>()); // 8
Console.WriteLine(Unsafe.SizeOf<MyStruct>()); // 16
info.SomeValue = 0x12345678;
info.Name = "Test";
var p = Marshal.AllocCoTaskMem(100);
var ptr = p.ToPointer();
Unsafe.Write(ptr, info);
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
public uint SomeValue;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)] // includes terminating zero
public string Name;
}
}
And debug it with a breakpoint after the Unsafe.Write method, open a memory window and use ptr as the start address, and you'll see this:
The SomeValue member is visible, but the Name is not, because ptr points to the managed object (with a probable leading pointer to something internal), not to it's P/invoke "projection".
Also, note in this case Marshal.SizeOf (P/Invoke size = 8) and Unsafe.SizeOf (Managed size = 16) results are different.
What type of string you're trying to read?
You can use the fixed modifier to read raw byte/char array in a struct:
public fixed byte Name[5];
Note that in .NET, all char/string is in Unicode, with each character taking 2 bytes. If you're trying to read an ascii/ansi string you should read them as bytes then use Encoding.ASCII.GetString to convert it to managed string. You should read them as char array if you're using wchar_t in unmanged code.
If the struct does not contain the actual string, but a pointer(char*/wchar_t*). You can read it as IntPtr then convert it with Marshal.PtrToString*
P.S.
Instead of using Unsafe, you can just read the struct with pointers:
var myStruct = *(SYMBOL_INFO_V*)pSymInfo;
I have a mouse and a touchpad on my laptop. I want a global hook for mousemove that tells me not only the new position, but also the physical device where it came from. Subscribing the hook only to the touchpad would be an even better solution, since I'm only interested in the touchpad. The hook has to work system wide (even if my application is not in focus).
How can I do this?
I'm not afraid to use Pinvoke in my C#/WPF application
You could use
P-Invoke user32.getrawinputdeviceinfo .
For mouse input you can get thing such:
[StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_MOUSE
{
[MarshalAs(UnmanagedType.U4)]
public int dwId;
[MarshalAs(UnmanagedType.U4)]
public int dwNumberOfButtons;
[MarshalAs(UnmanagedType.U4)]
public int dwSampleRate;
[MarshalAs(UnmanagedType.U4)]
public int fHasHorizontalWheel;
}
and, about Device:
StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_HID
{
[MarshalAs(UnmanagedType.U4)]
public int dwVendorId;
[MarshalAs(UnmanagedType.U4)]
public int dwProductId;
[MarshalAs(UnmanagedType.U4)]
public int dwVersionNumber;
[MarshalAs(UnmanagedType.U2)]
public ushort usUsagePage;
[MarshalAs(UnmanagedType.U2)]
public ushort usUsage;
}
I have a C++ .dll (unable to make changes to the code) from which I am trying to call a function that takes a reference parameter of a Struct type (also defined in the .dll).
The function requires the Struct to have the 'paramName' and 'groupName' fields populated, and based on those fields, it will return the Struct with the remaining fields populated.
I am not getting any marshaling errors, however, the library call returns an error code and a debug log shows that it is receiving an empty string for the two string fields (see below for how fields are set). My assumption is that the size of this struct is not aligning between the managed and unmanaged representation, and thus the struct is not blittable.
Here is the C++ method signature:
int GetConfigs(int contextHandle, Configs* configs);
And the C++ Configs struct:
struct Configs {
int myInt;
float myFloat;
bool flag;
char name[64];
char group[64];
}
The C# function wrapper:
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int GetConfigs(int contextHandle, [MarshalAs(UnmanagedType.Struct), In, Out] ref Configs configs);
The C# struct definition:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Configs
{
public int myInt;
public float myFloat;
[MarshalAs(UnmanagedType.U1)]
public bool flag;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string group;
}
As per the Microsoft documentation I have declared the C++ char[]'s to be represented in C# by strings and marshaled as ByValTStr with a set size.
Also from Microsoft documentation:
bool is not blittable, so I mark it with an explicit MarshalAs.
float is also not blittable, but declaring these fields as a float or a double made no difference in the original issue.
Lastly, the C# calling code:
var configs = new Library.Configs
{
name = "testName",
group = "testGroup"
};
var returnCode = Library.GetConfigs(GetContextId(), ref configs);
The return code comes back as a failure code and the library debug output file shows that the struct argument had:
name = []
(name is clearly set to what I expect it to be at the time of the call when I debug through the C# code)
Instead of declaring the string fields as
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string name;
I tried:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public byte[] name;
but that also made no difference.
The float/double was what was throwing off the struct byte size- the library was expecting a 4 byte floating point number but the data marshaler by default keeps those as 8 bytes. Fixing that along with the boolean as mentioned in the comments above solved the problem:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Configs
{
public int myInt;
[MarshalAs(UnmanagedType.R4)]
public float myFloat;
[MarshalAs(UnmanagedType.U1)]
public bool flag;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string group;
}
I need to write a text with glow in a Vista/seven glass window, and I'm, trying to call the API to write some text there. I have checked out a great sample in CodeProject, but the problem is that I'm using .NET 1 (please, don't ask :-)
I need to translate the follwing .NET 2 code to PInvoke, .NET 1 code.
// using System.Windows.Forms.VisualStyles
VisualStyleRenderer renderer = new VisualStyleRenderer(
VisualStyleElement.Window.Caption.Active);
// call to UxTheme.dll
DrawThemeTextEx(renderer.Handle,
memoryHdc, 0, 0, text, -1, (int)flags,
ref textBounds, ref dttOpts);
The class VisualStyleRenderer does not exist in .NET 1, so I need to get the renderer.Handle in other way.
Define the OpenThemeData API and DrawThemeTextEx, as well as some required structs and constants:
[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr OpenThemeData(IntPtr hwnd, string pszClassList);
[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
private extern static Int32 DrawThemeTextEx(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, string pszText, int iCharCount, uint flags, ref RECT rect, ref DTTOPTS poptions);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct DTTOPTS
{
public int dwSize;
public int dwFlags;
public int crText;
public int crBorder;
public int crShadow;
public int iTextShadowType;
public int ptShadowOffsetX;
public int ptShadowOffsetY;
public int iBorderSize;
public int iFontPropId;
public int iColorPropId;
public int iStateId;
public bool fApplyOverlay;
public int iGlowSize;
public IntPtr pfnDrawTextCallback;
public IntPtr lParam;
}
// taken from vsstyle.h
private const int WP_CAPTION = 1;
private const int CS_ACTIVE = 1;
And then, call it like this:
IntPtr handle = OpenThemeData(IntPtr.Zero, "WINDOW");
DrawThemeTextExt(handle, hdc, WS_CAPTION, CS_ACTIVE, ...)
The WS_CAPTION and CS_ACTIVE values match .NET 2's Caption and Active respectively. Values are described here officially: Parts and States
In short, you get what you want by calling OpenThemeData().
To work out all the details it would be much easier for you to write a sample app in C++ to get to know how to drive the theme API from the ground up. There are many tutorials and lots of sample code on the web. But do it in C++ where you will have all the functions readily available. The last thing you want to get doing is fighting with P/Invokes whilst you are also getting to grips with low-level theme API.
Once you get it cracked in C++, then move on to the P/Invokes and if you have trouble it will be easy to refer back to the C++ code that works.
Im trying to use SendInput to simulate keyboard presses in my app and want to support both 32-bit and 64-bit.
I've determined that for this to work, I need to have 2 different INPUT structs as such
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public ushort wVk; // Virtual Key Code
public ushort wScan; // Scan Code
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Explicit, Size = 28)]
public struct INPUT32
{
[FieldOffset(0)]
public uint type; // eg. INPUT_KEYBOARD
[FieldOffset(4)]
public KEYBDINPUT ki;
}
[StructLayout(LayoutKind.Explicit, Size = 40)]
public struct INPUT64
{
[FieldOffset(0)]
public uint type; // eg. INPUT_KEYBOARD
[FieldOffset(8)]
public KEYBDINPUT ki;
}
I wanted to know if there was a way to set the StructLayout size and FieldOffsets at runtime so I could use just one INPUT struct and determine the size and fieldoffset depending on the machine.
I have tried the code below but I would like to know if the same is possible at runtime instead of compile time.
#if _M_IX86
[StructLayout(LayoutKind.Explicit, Size = 28)]
#else
[StructLayout(LayoutKind.Explicit, Size = 40)]
#endif
public struct INPUT
{
[FieldOffset(0)]
public uint type; // eg. INPUT_KEYBOARD
#if _M_IX86
[FieldOffset(4)]
#else
[FieldOffset(8)]
#endif
public KEYBDINPUT ki;
}
Unfortunately, no.
Attributes are "fused" to the type at compile-time, which is why all values passed to an attribute constructor must be constants.
And at runtime you can't modify the attributes attached to the type. You can grab a copy and modify its values, but the actual attribute attached to the type will remain unchanged, so you can't "trick" mscorlib code into seeing your changes instead of the original, either.
You could always have 2 structs and determine which one to use at runtime.
With a proper design you could limit code duplication to a few lines. (Plus having the structures twice.)