R – How to pInvoke the function SHCameraCapture (camera dialog) from Aygshell.dll from WinMO6

camerapinvokewindows-mobile

I need to show a camera capture dialog in a compact framework 3.7 application by pinvoking SHCameraCapture from the dll Aygshell.dll. I cannont use the managed object CameraCaptureDialog because of limitations with the technology I'm working with. Instead, I need to access it by Pinvoking it.

See http://msdn.microsoft.com/en-us/library/aa454995.aspx for documentation on the function. The function takes in a struct that defines the parameters of the dialog. e.g. where to save the file, what resolution to use.

I would imaging that I would have to define a copy of the struct in C# and decorate the sturct with the attribute StructLayout. I also imagine that the code would involve [DllImport("aygshell.dll")]. Any sample code of how to call this would be much appreciated.

Best Answer

this code works....

#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 "";
}