我如何从WinMO6的Aygshell.dll中调用功能SHCameraCapture(相机对话框)

时间:2020-03-06 14:44:54  来源:igfitidea点击:

我需要通过从dll Aygshell.dll中调用SHCameraCapture,在紧凑型框架3.7应用程序中显示一个摄像机捕获对话框。由于我正在使用的技术的限制,我无法使用托管对象CameraCaptureDialog。相反,我需要通过点名访问它。

有关该功能的文档,请参见http://msdn.microsoft.com/zh-cn/library/aa454995.aspx。该函数采用定义对话框参数的结构。例如在哪里保存文件,使用什么分辨率。

我想像一下,我将不得不在Cand中定义该结构的副本,以使用属性StructLayout装饰该结构。我还可以想象该代码将涉及[DllImport(" aygshell.dll")]。任何如何调用此示例代码将不胜感激。

解决方案

我无法对此进行测试,但是struct / function应该看起来像这样:

struct SHCAMERACAPTURE {
  public Int32 cbSize;
  public IntPtr hwndOwner;

  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]  
  public string szFile;

  [MarshalAs(UnmanagedType.LPStr)] 
  string pszInitialDir;

  [MarshalAs(UnmanagedType.LPStr)] 
  string pszDefaultFileName;

  [MarshalAs(UnmanagedType.LPStr)] 
  string pszTitle;

  Int32 StillQuality;
  Int32 VideoTypes;
  Int32 nResolutionWidth;
  Int32 nResolutionHeight;
  Int32 nVideoTimeLimit;
  Int32 Mode;
}

[DllImport("aygshell.dll")]
static extern int SHCameraCapture(ref SHCAMERACAPTURE pshcc);

AFAIK,我们无需在SHCAMERCAPTURE上显式设置StructLayout,因为其布局没有任何异常。

一旦完成这项工作,我们可能希望将发现发布到pinvoke.net上,以供他人使用!

不好意思,这是一个不错的开始...感谢我们开始使用。我尝试将其连接起来,但是得到了NotSupportedException。

我从下面的测试应用程序中粘贴了文本。请注意,我尝试用[StructLayout(LayoutKind.Sequential)]装饰该结构。我还公开了所有成员,以消除对象可访问性方面的任何问题。

public partial class Form1 : Form
{
    [DllImport("aygshell.dll")]
    static extern int SHCameraCapture(ref SHCAMERACAPTURE pshcc);

    [StructLayout(LayoutKind.Sequential)]
    struct SHCAMERACAPTURE
    {
        public Int32 cbSize;
        public IntPtr hwndOwner;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szFile;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pszInitialDir;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pszDefaultFileName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pszTitle;
        public Int32 StillQuality;
        public Int32 VideoTypes;
        public Int32 nResolutionWidth;
        public Int32 nResolutionHeight;
        public Int32 nVideoTimeLimit;
        public Int32 Mode;
    }
    private void ShowCamera()
    {
        SHCAMERACAPTURE captureData = new SHCAMERACAPTURE
                                           {
                                               cbSize = sizeof (Int64),
                                               hwndOwner = (IntPtr)0,
                                               szFile = "\My Documents",
                                               pszDefaultFileName = "picture.jpg",
                                               pszTitle = "Camera Demo",
                                               StillQuality = 0,
                                               VideoTypes = 1,
                                               nResolutionWidth = 480,
                                               nResolutionHeight = 640,
                                               nVideoTimeLimit = 0,
                                               Mode = 0
                                           };
        SHCameraCapture(ref captureData);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        ShowCamera();
    }

此代码有效....

#region Enumerations

public enum CAMERACAPTURE_STILLQUALITY
{
    CAMERACAPTURE_STILLQUALITY_DEFAULT = 0,
    CAMERACAPTURE_STILLQUALITY_LOW = 1,
    CAMERACAPTURE_STILLQUALITY_NORMAL = 2,
    CAMERACAPTURE_STILLQUALITY_HIGH = 3
}
public enum CAMERACAPTURE_VIDEOTYPES
{
    CAMERACAPTURE_VIDEOTYPE_ALL = 0xFFFF,
    CAMERACAPTURE_VIDEOTYPE_STANDARD = 1,
    CAMERACAPTURE_VIDEOTYPE_MESSAGING = 2
}

public enum CAMERACAPTURE_MODE
{
    CAMERACAPTURE_MODE_STILL = 0,
    CAMERACAPTURE_MODE_VIDEOONLY = 1,
    CAMERACAPTURE_MODE_VIDEOWITHAUDIO = 2
}

#endregion //Enumerations

#region Structures

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct struSHCAMERACAPTURE
{
    public uint cbSize;
    public IntPtr hwndOwner;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public String szFile;
    [MarshalAs(UnmanagedType.LPTStr)]
    public String pszInitialDir; //LPCTSTR
    [MarshalAs(UnmanagedType.LPTStr)]
    public String pszDefaultFileName; //LPCTSTR
    [MarshalAs(UnmanagedType.LPTStr)]
    public String pszTitle;  //LPCTSTR
    public CAMERACAPTURE_STILLQUALITY StillQuality;
    public CAMERACAPTURE_VIDEOTYPES VideoTypes;
    public uint nResolutionWidth;
    public uint nResolutionHeight;
    public uint nVideoTimeLimit;
    public CAMERACAPTURE_MODE Mode;
}
#endregion //Structures

#region API
[DllImport("Aygshell.dll", SetLastError = true,CharSet=CharSet.Unicode)]
public static extern int SHCameraCapture
(
    ref struSHCAMERACAPTURE pshCamCapture
);

private string StartImager(String strImgDir, String strImgFile, uint uintImgHeight,
                            uint uintImgWidth)

try
{
  struSHCAMERACAPTURE shCamCapture = new struSHCAMERACAPTURE();

  shCamCapture.cbSize = (uint)Marshal.SizeOf(shCamCapture);
  shCamCapture.hwndOwner = IntPtr.Zero;
  shCamCapture.szFile = "\" + strImgFile;  //strImgDir + "\" + strImgFile;
  shCamCapture.pszInitialDir = "\"; // strImgDir;
  shCamCapture.pszDefaultFileName = strImgFile;
  shCamCapture.pszTitle = "PTT Image Capture";
  shCamCapture.StillQuality = 0; // CAMERACAPTURE_STILLQUALITY.CAMERACAPTURE_STILLQUALITY_NORMAL;
  shCamCapture.VideoTypes = CAMERACAPTURE_VIDEOTYPES.CAMERACAPTURE_VIDEOTYPE_STANDARD;
  shCamCapture.nResolutionHeight = 0; // uintImgHeight;
  shCamCapture.nResolutionWidth = 0; // uintImgWidth;
  shCamCapture.nVideoTimeLimit = 10;
  shCamCapture.Mode = 0; // CAMERACAPTURE_MODE.CAMERACAPTURE_MODE_STILL;

  //IntPtr intptrCamCaptr = IntPtr.Zero;

  //Marshal.StructureToPtr(shCamCapture, intptrCamCaptr, true);

  int intResult = SHCameraCapture(ref shCamCapture);
  if (intResult != 0)
  {
      Win32Exception Win32 = new Win32Exception(intResult);
      MessageBox.Show("Error: " + Win32.Message);
  }             

  return strCaptrErr;
}

catch (Exception ex)
{
    MessageBox.Show("Error StartImager : " + ex.ToString() + " - " + strCaptrErr
                    , "Nomad Imager Test");
    return "";
}