zoukankan      html  css  js  c++  java
  • Problem in calling C++ dll from C# Code

    Hi!

    I am calling a C++ dll function from my C# code . but facing the problem in a function called inside that C++ function.

    That's how I'm referring to the C++ dll function inside my C# class :
    Code:
    class TestInterop { [DllImport("DBApi.dll", EntryPoint="DeleteRecFromDB", ExactSpelling=false,SetLastError=true, CharSet=CharSet.Unicode)] internal extern static int DeleteRecFromDB([MarshalAs(UnmanagedType.LPWStr)]string empName,[MarshalAs(UnmanagedType.LPWStr)]string deptName); public TestInterop(){} static void Main(string[] args) { TestInterop.DeleteRecFromDB("KT","Dep-01"); } }

    Now my C++ function is written in DBApi.cpp as:
    Code:
    DBApiReturns DeleteRecFromDB(CString empID, CString depName){ try { int returns; //check for required field if ( CheckRequiredField(empID) == FALSE ) return MissingRequiredField; if ( CheckRequiredField(depName) == FALSE ) return MissingRequiredField; pServerInterface->DeleteRec(CComBSTR(empID),CComBSTR(depName),&returns); if( returns != 1) return (Success); else return(Failure); } catch(_com_error &ce) { return COMError ; } catch(...) { return UnknownError ; } }

    When I call this function , I get 'UnknownError' on the function CheckRequiredField().If I bypass this function the code works fine.CheckRequiredField() is define as following:
    Code:
    CheckRequiredField(CString field) { if ( field.GetLength() <= 0) return FALSE; return TRUE; }

    I suspect that there must be some problem in the way I'm doing the parameter marshalling.

    Please Help.

    thanks and regards
    KT.
  • 相关阅读:
    EBS_FORM_开发:关于离开record验证
    EBS_FORM_开发:关于WHEN-VALIDATE-ITEM上面commit
    EBS_FORM_开发:关于LOCK-ON
    EBS_FORM_开发:实现form record ctrl+F6复制
    EBS_FORM_开发:关于切换不同BLOCK的时候弹出需要保存的窗口
    EBS_FORM_开发:FRM-40150的解决思路
    工作记录
    02_mysql表的操作
    Mysql_字符集_排序规则
    JAVA_IO
  • 原文地址:https://www.cnblogs.com/cy163/p/302980.html
Copyright © 2011-2022 走看看