Home  >  Q&A  >  body text

请问 c#如何调用c++的dll? 就比如下面这个

CString GetCurrentDir(void)
{
    TCHAR sDrive[_MAX_DRIVE];
    TCHAR sDir[_MAX_DIR];
    TCHAR sFilename[_MAX_FNAME];
    TCHAR Filename[_MAX_FNAME];
    TCHAR sExt[_MAX_EXT];
    
    GetModuleFileName(AfxGetInstanceHandle(),Filename,_MAX_PATH);
    _tsplitpath(Filename,sDrive,sDir,sFilename,sExt);
    CString HomeDir(CString(sDrive)+CString(sDir));    
    if (HomeDir.GetAt(HomeDir.GetLength()-1)!=_T('\\'))
        HomeDir+=_T('\\');
    return HomeDir;
}


    char path[256];
    sprintf(path, "%splaylist1.txt", GetCurrentDir());
    result = AddWindowEx(iCom, iAddress, iSchedule, 0, 0, 128, 32, path);
    if (!result)
        AfxMessageBox("设置播放数据playlist1.txt失败");

我怎么调用AddWindowEx 这个方法
主要是 sprintf(path, "%splaylist1.txt", GetCurrentDir()); 这一句是干嘛的 百度了一圈也没看到答案 有没有c++大神 帮忙看看

怪我咯怪我咯2762 days ago692

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-17 13:56:23

    sprintf can be replaced by C#'s string.Format(). If you use C#6, you can directly write the template string $"", and GetCurrentDir should be replaced by Environment.CurrentDirectory.

    var path = string.Format("{0}playlist1.txt", GetCurrentDir());
    path = $"{GetCurrentDir()playlist1.txt}";

    Is AddWindowEx a custom function?

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:56:23

    c# can only call c functions through the platform

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:56:23

    using System.Runtime.InteropServices;
    [DLLImport("xxx.dll")]
    public static extern 返回值 方法名(参数);

    Then just call the method directly

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:56:23

    String formatting function, encyclopedia has entry

    reply
    0
  • Cancelreply