title: Import c++ unmanaged dll to c# problem
body: <p>i have this code in c++</p>
  
  <pre><code>   extern "C"
  {
    __declspec(dllexport) void ConvertToByte(char *filename,unsigned char* data)
  
    {
         BITMAPINFO *info;
        data=(unsigned char *)LoadDIBitmap(filename, &amp;info); 
  
    }
  }
  </code></pre>
  
  <p>i want from c# to give the filename and take the data
  how?</p>
  
  <pre><code> [DllImport("bmpToByte.dll", CharSet = CharSet.Ansi)]
      public static extern void ConvertToByte(string pame,ref byte[] data);
  </code></pre>
  
  <p>This way doesn't work.Any idea please help because i am new in programming and i have no idea how to do it.Thanks
  I send all the code that i use from c++.Maybe someone has an idea.Please give some help.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;memory.h&gt;
  #include &lt;windows.h&gt;
  
  
  void *
  LoadDIBitmap(char       *filename, /* I - File to load */
               BITMAPINFO **info)    /* O - Bitmap information */
  {
    FILE             *fp;            /* Open file pointer */
    void             *bits;          /* Bitmap pixel bits */
    long             bitsize,        /* Size of bitmap */
                     infosize;       /* Size of header information */
    BITMAPFILEHEADER header;         /* File header */
  
   /*
    * Try opening the file; use "rb" mode to read this *binary* file.
    */
  
    if ((fp = fopen(filename, "rb")) == NULL)
      return (NULL);
  
   /*
    * Read the file header and any following bitmap information…
    */
  
    if (fread(&amp;header, sizeof(BITMAPFILEHEADER), 1, fp) &lt; 1)
    {
     /*
      * Couldn't read the file header - return NULL…
      */
  
      fclose(fp);
      return (NULL);
    };
  
    if (header.bfType != 'MB') /* Check for BM reversed… */
    {
     /*
      * Not a bitmap file - return NULL…
      */
  
      fclose(fp);
      return (NULL);
    };
    infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
    if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
    {
     /*
      * Couldn't allocate memory for bitmap info - return NULL…
      */
  
      fclose(fp);
      return (NULL);
    };
  
    if (fread(*info, 1, infosize, fp) &lt; infosize)
    {
     /*
      * Couldn't read the bitmap header - return NULL…
      */
  
      free(*info);
      fclose(fp);
      return (NULL);
    };
  
   /*
    * Now that we have all the header info read in, allocate memory for the
    * bitmap and read *it* in…
    */
  
    if ((bitsize = (*info)-&gt;bmiHeader.biSizeImage) == 0)
      bitsize = ((*info)-&gt;bmiHeader.biWidth *
                 (*info)-&gt;bmiHeader.biBitCount + 7) / 8 *
          abs((*info)-&gt;bmiHeader.biHeight);
  
    if ((bits = malloc(bitsize)) == NULL)
    {
     /*
      * Couldn't allocate memory - return NULL!
      */
  
      free(*info);
      fclose(fp);
      return (NULL);
    };
  
    if (fread(bits, 1, bitsize, fp) &lt; bitsize)
    {
     /*
      * Couldn't read bitmap - free memory and return NULL!
      */
  
      free(*info);
      free(bits);
      fclose(fp);
      return (NULL);
    };
  
   /*
    * OK, everything went fine - return the allocated bitmap…
    */
  
    fclose(fp);
    return (bits);
  }
  
  
  int
  SaveDIBitmap(char       *filename, /* I - File to save to */
               BITMAPINFO *info,     /* I - Bitmap information */
               void       *bits)     /* I - Bitmap pixel bits */
  {
    FILE             *fp;            /* Open file pointer */
    long             size,           /* Size of file */
                     infosize,       /* Size of bitmap info */
                     bitsize;        /* Size of bitmap pixels */
    BITMAPFILEHEADER header;         /* File header */
  
   /*
    * Try opening the file; use "wb" mode to write this *binary* file.
    */
  
    if ((fp = fopen(filename, "wb")) == NULL)
      return (-1);
  
    if (info-&gt;bmiHeader.biSizeImage == 0)/* Figure out the bitmap size */
      bitsize = (info-&gt;bmiHeader.biWidth *
                 info-&gt;bmiHeader.biBitCount + 7) / 8 *
        abs(info-&gt;bmiHeader.biHeight);
    else
      bitsize = info-&gt;bmiHeader.biSizeImage;
  
    infosize = sizeof(BITMAPINFOHEADER);
    switch (info-&gt;bmiHeader.biCompression)
    {
      case BI_BITFIELDS :
          infosize += 12; /* Add 3 RGB doubleword masks */
          if (info-&gt;bmiHeader.biClrUsed == 0)
            break;
      case BI_RGB :
          if (info-&gt;bmiHeader.biBitCount &gt; 8 &amp;&amp;
              info-&gt;bmiHeader.biClrUsed == 0)
            break;
      case BI_RLE8 :
      case BI_RLE4 :
          if (info-&gt;bmiHeader.biClrUsed == 0)
            infosize += (1 &lt;&lt; info-&gt;bmiHeader.biBitCount) * 4;
          else
            infosize += info-&gt;bmiHeader.biClrUsed * 4;
          break;
    };
  
    size = sizeof(BITMAPFILEHEADER) + infosize + bitsize;
  
   /*
    * Write the file header, bitmap information, and bitmap pixel data…
    */
  
    header.bfType      = 'MB'; /* Non-portable… sigh */
    header.bfSize      = size;
    header.bfReserved1 = 0;
    header.bfReserved2 = 0;
    header.bfOffBits   = sizeof(BITMAPFILEHEADER) + infosize;
    if (fwrite(&amp;header, 1, sizeof(BITMAPFILEHEADER), fp) &lt;
            sizeof(BITMAPFILEHEADER))
    {
     /*
      * Couldn't write the file header - return…
      */
  
      fclose(fp);
      return (-1);
    };
    int l;
    if (l=fwrite(info, 1, infosize, fp) &lt; infosize)
    {
     /*
      * Couldn't write the bitmap header - return…
      */
  
      fclose(fp);
      return (-1);
    };
  
    if (l=fwrite(bits, 1, bitsize, fp) &lt; bitsize)
    {
     /*
      * Couldn't write the bitmap - return…
      */
  
      fclose(fp);
      return (-1);
    };
  
   /*
    * OK, everything went fine - return…
    */
  
    fclose(fp);
    return (0);
  }
  
  extern "C"
  {
    __declspec(dllexport) void ConvertToByte(char *filename,unsigned char** data)
  
    {
        char temp[100];
        sprintf(temp,filename);
         BITMAPINFO *info;
        *data=(unsigned char *)LoadDIBitmap(temp, &amp;info); 
  
  
    }
  </code></pre>