Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem: I create a C++ dll which contains a message box, as follows:
C#
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
int _stdcall f()
{
    double dNum = 3.45;
    char str[33];
    sprintf_s(str,"The solution is %lf",dNum);
    MessageBox(NULL,(LPCWSTR)str,(LPCWSTR)"Thong bao",MB_OK);
    return 0;
}


I compile successfully, the name of this dll is "TestDll" and put it into System32 folder (Windows 7), I call f() function to display message box above from C#:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("TestDll.dll")]
        public static extern int f();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int ret;
            ret = f();
        }
    }
}


But I dont recieve any message box. How to solve this. Hope you help me. Thanks.
Posted
Comments
Pascal-78 4-Jul-13 4:11am    
Did you check with "Dependency Walker" (depends.exe) if your f function is exported.
You should specify __declspec(dllexport) on your f function in the C++ file to export it.
Andrewpeter 4-Jul-13 4:23am    
Thanks, I used Depends.exe to check export of that function, it's exported. I have done follow what you supported, but it doesnt work! Hope that I have another solution of you. Thanks.
Pascal-78 4-Jul-13 5:15am    
To check if your function is called, you can insert a call to "OutputDebugString" and check if your string is displayed in the debug windows of VS (or in the dbmon output window).
You may also try to call InitCommonControlsEx first. (http://msdn.microsoft.com/en-us/library/bb775697%28v=vs.85%29.aspx)
thanh_bkhn 4-Jul-13 4:13am    
Try to use AfxMessageBox instead, and tell me the result?
Andrewpeter 4-Jul-13 4:28am    
Thanks my countryman, I used AfxMessageBox() to instead of MessageBox(), but I can not know how to do that, I think the AfxMessageBox() is only used in MFC, do you agree with me about this? If I am wrong you can help me solve this, thanks.

I would check MessageBox function return value, and call GetLastError if it is equal to zero (you might eventually return the error code to the caller).
 
Share this answer
 
Comments
Andrewpeter 4-Jul-13 4:35am    
Thank you very much CPallini, how to check return value of that function?
CPallini 4-Jul-13 4:43am    
int error = 0;
if ( MessageBox(NULL,(LPCWSTR)str,(LPCWSTR)"Thong bao",MB_OK) == 0 )
{
error = GetLastError();
}
return error;
Andrewpeter 4-Jul-13 4:46am    
OK, thanks. How to solve this? You can help me?
CPallini 4-Jul-13 4:54am    
How can I possibly help you without knowing the error number you're getting?
Andrewpeter 4-Jul-13 5:00am    
I want to display a message box as I ask above. I think you can help me do that. OK? Thanks.
You cannot cast a char[] to PCWSTR in the argument list of your MessageBox call. That's probably the reason why you don't see the message box.

Use wchar_t and swprintf instead. And for your string constants use L"...".
 
Share this answer
 
Comments
Andrewpeter 4-Jul-13 4:40am    
Oh, I tryed doing as follow your support, e.g:
MessageBox(NULL,(LPCWSTR)L"I love Vietnam",(LPCWSTR)L"Report...",MB_OK);
But it doesnt work! What is happened? Thanks.
nv3 4-Jul-13 4:48am    
I would put a breakpoint on the MessageBox line, build everything in the "debug" configuration and start by pressing F5 in Visual Studio to start the debugger. Then you will see if you arrive in your function f at all.
I don't believe you created the correct linkage for the API. Besides the type conversion mentioned in solution #2, you probably also want to declare f() as 'extern "C"' and __declspec(dllexport) to call the way you want from C#. I don't see any sign that you did this either in the .cpp file or a .h file.

When you build the dll, check that the decorated name matches what you specify in the C# code. You can open your dll in depends.exe (eg. for vs2010, click Start -> Visual Studio 2010 -> Visual Studio Tools -> depends.exe). If the API doesn't show up in the second pane on the right, you didn't export it correctly. If the signature differs from the C# declaration, you need to make them match.
 
Share this answer
 
Comments
nv3 4-Jul-13 11:49am    
Good points. Perhaps OP already did that; at least that's what I understood from his first comments below his question.

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