Click here to Skip to main content
15,607,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a job that needs to process 16-bit images so that they can be displayed normally in ASP.NET webForm.
The professor gave me a C++ file, which is a code written by the professor’s prospective doctoral student.
Because I don’t have enough experience with C++ and C#, I tried for a week and still no results.
The file content given by the professor includes .pdb, .lib, .ilk, .exp, .dll
In the .h file, it can see the code, And I know that C# needs to read the code from the .dll.

This is the code in C++ .h file.

struct CImage_DLL
	int m_nW, m_nH;
	short* pshImageData;
	CImage_DLL() : m_nW(0), m_nH(0), pshImageData(nullptr){}
extern "C" MoveFullImageByFirstImage_API CImage_DLL* Open16BitGrayImage(
	const char* strSrcImageFilePath);

What I have tried:

And this is the code in C#

     public Form1()


     [DllImport("D:\\image_16bit_code\\MoveFullImageByFirstImage.dll", CallingConvention = CallingConvention.Cdecl)]
     public static extern IntPtr Open16BitGrayImage(string imgFile);

     private void button7_Click(object sender, EventArgs e)
         string imgUrl = "D:\\image_16bit_code\\Image Mapping\\MRI-PDI_Axial-Z\\MRI-PDI_Axial-Z_0110.png";


         CImage_DLL _DLL = (CImage_DLL)Marshal.PtrToStructure(Open16BitGrayImage(imgUrl), typeof(CImage_DLL));

// [StructLayout(LayoutKind.Auto)]
 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 public struct CImage_DLL
     public int m_nW, m_nH;//影像寬高

     [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I2)]
     public IntPtr[] pshImageData;//16Bit的灰階影像資料

I tried many different variables and many people’s different methods. Configuration build has tried to use x86 and x64.
The error code always appears (Exception from HRESULT: 0x8007000B)

When the image path is correct, all I can think of is the problem with the definition of the variable of an array in the structure. The value in the array will be one-dimensional, and the size is an array of image length * width, The value is 16bit.

Any help? I have no idea what I need to do to read the value I need?
Updated 2-Jun-21 5:48am

You cannot pass a C# string object to a C function that expects a const char*. You need to use one of the Marshal class functions, such as Marshal.StringToBSTR(String) Method (System.Runtime.InteropServices) | Microsoft Docs[^], to convert it to something that the C code can read.
Share this answer
ms0482828 2-Jun-21 22:42pm    
Thanks for your help, Richard McCutchan. But the project still doesn't work
I have tried to change the path and use Marshal.StringToBSTR(String) Method like this:

[DllImport("D:\\image_16bit_code\\MoveFullImageByFirstImage.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Open16BitGrayImage(IntPtr imgFile);

string imgUrl = "Image_Mapping/MRI-PDI_Axial-Z/MRI-PDI_Axial-Z_0110.png";
IntPtr BSTR_imgUrlt = Marshal.StringToBSTR(imgUrl);

Am I using it incorrectly? Error still exists.
char * whether the return should not be used IntPtr?
Richard MacCutchan 3-Jun-21 3:05am    
I suspect the issue may be that the C++ dll is the cause of the problem. You need to get together with the person who wrote it to find out exactly what target CPU it has been made for.
ms0482828 3-Jun-21 9:15am    
Understand, thank you very much :D
I think there is only this way, the error message makes me tired
These nasty HR error leads to some installation issue. Try to interact with plain data like strings at first to EXCLUDE installation issues.

When I see this hard coded string than I know that your code is evil. Use UNC pathes or copy ALL files into one "working directory" in which your processe runs. And try to narrow the code where an error can occur.

AND: DONT mix x86 and x64 files and pray for the best. Its waisted time - it wont work!!!
Share this answer
ms0482828 2-Jun-21 22:50pm    
Thanks for your help, KarstenK. I have put the data in the same folder, and create an empty project to test. I think there is no have mixed x86 and x64 files in the empty project. But the Error still exists.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900