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

I have created a C# win Form application which uses a C++ dll. This is my first attempt in C++ programming. I have called one function in that dll from C# application. Seems like everyrhing working fine.

The problem is, the C++ function called from C# application has another function call inside that. This inner function call does not execute.

I don't know how to achieve that.
Thank you.

What I have tried:

C# Code
[DllImport("abc.dll", CallingConvention = CallingConvention.Cdecl)]
     public static extern IntPtr work1();
      private void btnwork1_Click(object sender, EventArgs e)
        {
            IntPtr array = work1();
            .
            .
            .
        }



C++ Code
C++
int CheckLicense()
{.
 .
 .
}

extern "C" {  
    __declspec(dllexport)  char* wok1()
     {
       .
       .
       .
       CheckLicense();  // This Function not called.
                        // If i put the code of CheckLicense() inside this function
                        // Works fine.
       .
       .
       .
      }
    }


I don't know the right way.
Posted
Updated 24-Jan-21 9:47am
Comments
CPallini 24-Jan-21 4:33am    
It is strange that C# code calling 'work1' is able to invoke C++ function named 'wok1' instead. That is: show us the relevant part of your actual code.
Member 14837660 24-Jan-21 5:00am    
Sorry. It is typing mistake.
CPallini 24-Jan-21 5:13am    
All right, but it implies you're not showing us the actual code.
Member 14837660 24-Jan-21 5:20am    
Yeah. Since it is a commercial product, i could not. I hope you understand.
The inner function CheckLicense() is responsible for collecting
1.HDD Serial Number
2.BIOS Serial Number
3.Motherboard Serial Number
To achieve that it uses
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------

hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);


if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}

While debugging the following error occurs.
>Failed to initialize COM library. Error code = 0x80010106
>Failed to initialize security. Error code = 0x80010119
(return 1)

Now, when i googled, it says "The COM Library can not be initialized by dll. It can only by Exe.
So is there any way to achieve my goal?
Thank you.

This cannot be. If you called the function, the inner function will be called. Why do you think it's not being called? I think you are mistaken
 
Share this answer
 
Comments
Member 14837660 24-Jan-21 4:30am    
Thank you Mr. Christian Graus,
You are right. The inner function CheckLicense() is responsible for collecting
1.HDD Serial Number
2.BIOS Serial Number
3.Motherboard Serial Number
To achieve that it uses
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------

hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);


if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}

While debugging the following error occurs.
>Failed to initialize COM library. Error code = 0x80010106
>Failed to initialize security. Error code = 0x80010119
(return 1)

Now, when i googled, it says "The COM Library can not be initialized by dll. It can only by Exe.
So is there any way to achieve my goal?
Thank you.
CPallini 24-Jan-21 4:33am    
5.
Your code looks correct. It is not the same as the code that has a problem. Make a sample project that reproduces the problem and put it on GitHub and I'll have a look.

You have a problem with the returned value of char* from wok1()? Show the code of how you derived the char* value after CheckLicense() call.
 
Share this answer
 
Comments
Member 14837660 24-Jan-21 4:33am    
Thank you Mr. Shao Voon Wong,
No, i don't have any issue on collecting the returned value. I got the expected return value.

The inner function CheckLicense() is responsible for collecting
1.HDD Serial Number
2.BIOS Serial Number
3.Motherboard Serial Number
To achieve that it uses
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------

hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);


if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}

While debugging the following error occurs.
>Failed to initialize COM library. Error code = 0x80010106
>Failed to initialize security. Error code = 0x80010119
(return 1)

Now, when i googled, it says "The COM Library can not be initialized by dll. It can only by Exe.
So is there any way to achieve my goal?
Thank you.
Shao Voon Wong 24-Jan-21 4:46am    
WinForm and WPF already call CoInitialize/CoUninitialize on your application's behalf. If you call CoInitialize() again with a different apartment, it will fail. I am not sure about CoInitializeSecurity: I suppose you can call it in DllMain() 's DLL_PROCESS_ATTACH. You can use a COM object in C# directly than going through pinvoke route. You can google or read the documentation of your COM component.
CPallini 24-Jan-21 4:34am    
5.
When the inner code isnt executed than you must search the error on the place in your code. Take a look at my article about calling C++ in C#.
 
Share this answer
 
Comments
CPallini 24-Jan-21 4:34am    
5.
Member 14837660 24-Jan-21 5:38am    
Thank you Mr. KarstenK
The inner function CheckLicense() is responsible for collecting
1.HDD Serial Number
2.BIOS Serial Number
3.Motherboard Serial Number
To achieve that it uses
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------

hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);


if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}

While debugging the following error occurs.
>Failed to initialize COM library. Error code = 0x80010106
>Failed to initialize security. Error code = 0x80010119
(return 1)

Now, when i googled, it says "The COM Library can not be initialized by dll. It can only by Exe.
So is there any way to achieve my goal?
Thank you.
Thank you all for spending your precious time to help me.
With the help of your answers and suggestions I resolved the problem.
What i did is..
Since the [STAThread] is resposible for Initialization of COM Library
Removed the following functions from my CheckLicense() in the C++ dll
C++
CoInitializeEx(0, COINIT_MULTITHREADED);

C++
CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);

C++
CoUninitialize();

Now works fine.
Thanks a lot to @CPallini , @ChristianGraus , @wong-shao-voon , @KarstenK
 
Share this answer
 

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