Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字节[]转换为安全数组_C#_Arrays_Pointers_Pinvoke - Fatal编程技术网

C# 将字节[]转换为安全数组

C# 将字节[]转换为安全数组,c#,arrays,pointers,pinvoke,C#,Arrays,Pointers,Pinvoke,我有个问题,希望有人能帮我解决。以下代码用于在VS2012中编译: STDMETHODIMP CGatherStore::GetPathBmp(ULONGLONG sessionID, LONG *pWidth, LONG *pHeight, SAFEARRAY **pData) { m_dbOps.OpenDatabase(m_depository); if (m_dbOps.HasPath(sessionID)) { SessionData sd(m_dbOps.GetS

我有个问题,希望有人能帮我解决。以下代码用于在VS2012中编译:

STDMETHODIMP CGatherStore::GetPathBmp(ULONGLONG sessionID, LONG *pWidth, LONG *pHeight, SAFEARRAY **pData)
{
  m_dbOps.OpenDatabase(m_depository);
  if (m_dbOps.HasPath(sessionID))
  {
    SessionData sd(m_dbOps.GetSessionPath(sessionID));

    *pWidth  = sd.pathHeader.bcWidth;
    *pHeight = sd.pathHeader.bcHeight;

    CComSafeArray<BYTE> bmpArray;
    CComSafeArrayBound  bounds;

    *pData = SafeArrayCreate(VT_UI1, 1, &bounds);
    if (sd.spPathRawData.m_p != NULL)
    {
      bmpArray.Attach(*pData);
      bmpArray.Add(sd.GetPathSize(), reinterpret_cast<BYTE *>(sd.spPathRawData.m_p), true);
      bmpArray.Detach();
    }
  }
  else
  {
    CComSafeArrayBound bounds;

    *pData = SafeArrayCreate(VT_UI1, 1, &bounds);
  }

  return S_OK;
}
调用它的函数如下所示:

void GetPathBmp(ulong sessionid, out int pWidth, out int pHeight, out Array pData);
public WriteableBitmap GetBitmapPath(ulong sessionID)
{
  WriteableBitmap bmp = null;

  try
  {
    int    width;
    int    height;
    byte[] data;

    gs.GetPathBmp(sessionID, out width, out height, out data);
    bmp = BitmapFactory.New(width, height);
    bmp.FromByteArray(data);
  }
  catch (Exception e)
  {
    System.Diagnostics.Trace.WriteLine(String.Format("GetBitmapPath failed, session ID {0} - {1}", sessionID, e.Message));
  }

  return bmp;
}
public WriteableBitmap GetBitmapPath(ulong sessionID)
{
  WriteableBitmap bmp = null;
  try
  {
    int          width;
    int          height;
    System.Array sa;

    gs.GetPathBmp(sessionID, out width, out height, out sa);
    bmp = BitmapFactory.New(width, height);

    byte[] data = new byte[sa.Length];
    sa.CopyTo(data, 0);
    bmp.FromByteArray(data);
  }
  catch (Exception e)
  {
    System.Diagnostics.Trace.WriteLine(String.Format("GetBitmapPath failed, session ID {0} - {1}", sessionID, e.Message));
  }
  return bmp;
}
但当我尝试在VS2015中编译它时,我收到以下错误消息:

error CS1503: Argument 4: cannot convert from 'out byte[]' to 'out System.Array'
那么如何将变量从byte[]转换为SAFEARRAY呢


提前感谢。

我修改了调用函数,如下所示:

void GetPathBmp(ulong sessionid, out int pWidth, out int pHeight, out Array pData);
public WriteableBitmap GetBitmapPath(ulong sessionID)
{
  WriteableBitmap bmp = null;

  try
  {
    int    width;
    int    height;
    byte[] data;

    gs.GetPathBmp(sessionID, out width, out height, out data);
    bmp = BitmapFactory.New(width, height);
    bmp.FromByteArray(data);
  }
  catch (Exception e)
  {
    System.Diagnostics.Trace.WriteLine(String.Format("GetBitmapPath failed, session ID {0} - {1}", sessionID, e.Message));
  }

  return bmp;
}
public WriteableBitmap GetBitmapPath(ulong sessionID)
{
  WriteableBitmap bmp = null;
  try
  {
    int          width;
    int          height;
    System.Array sa;

    gs.GetPathBmp(sessionID, out width, out height, out sa);
    bmp = BitmapFactory.New(width, height);

    byte[] data = new byte[sa.Length];
    sa.CopyTo(data, 0);
    bmp.FromByteArray(data);
  }
  catch (Exception e)
  {
    System.Diagnostics.Trace.WriteLine(String.Format("GetBitmapPath failed, session ID {0} - {1}", sessionID, e.Message));
  }
  return bmp;
}

它似乎起作用了。谢谢大家的帮助。

你确定编译时使用的代码是什么吗?!在我看来,此组件的作者更改了此COM组件的IDL。因此,现在类型库导入器可以(正确地)看到它是一个字节数组。通常程序员认为这是一个解决方案,而不是一个问题,尽管修改接口而不改变[uUID] s是一个相当粗略的DLL地狱诱导器。与作者交谈或相应地修改您的客户代码。@HansPassant,我真的很感兴趣-您能解释一下“相应地修改您的客户代码”与我的回答(向他展示如何修改客户代码)有什么不同吗?我遗漏了什么?最重要的是,他得到的实际编译错误是针对C代码的,而不是针对互操作。。。直到运行时才会被捕获,对吗?@DavidG,不,我不确定这段代码是否用于编译。写这封信的人离开了公司。我接管了我认为完整的代码,因为我们有一个工作版本,我认为它是从我开始使用的代码创建的。如果有人对我应该做什么(包括使用不同的变量类型)提出建议,我愿意接受所有想法。