2012-01-17 18 views
6

से पास स्ट्रिंग पास मैं सी # से सी डीएलएल तक एक स्ट्रिंग पास करने की कोशिश कर रहा हूं। मैंने जो पढ़ा है, उससे .NET को मेरे लिए स्ट्रिंग से char * में रूपांतरण करना चाहिए, हालांकि मुझे "त्रुटि CS1503: तर्क '1' मिलता है: 'स्ट्रिंग' से 'char *' में परिवर्तित नहीं किया जा सकता है" क्या कोई मुझे सलाह दे सकता है कि मैं कहां हूं गलत हो गया है? धन्यवाद।सी # से सी डीएलएल

सी # कोड

[DllImport("Source.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] 
public static unsafe extern bool StreamReceiveInitialise(char* filepath); 

const string test = "test"; 
// This method that will be called when the thread is started 
public void Stream() 
{ 
    if (StreamReceiveInitialise(test)) 
    { 


    } 
} 

सी DLL

extern "C" 
{ 
    __declspec(dllexport) bool __cdecl StreamReceiveInitialise(char* filepath); 
} 
+1

क्यों असुरक्षित के रूप में StreamReceiveInitialise घोषित? अब आपको स्ट्रिंग के बजाए एक char * पास करने की आवश्यकता है। – diggingforfire

+0

मैंने अब असुरक्षित हटा दिया है और उत्तर 1 में दिखाए गए अनुसार उपयोग कर रहा हूं। – integra753

उत्तर

3

अपने बाहरी प्रणाली की घोषणा के रूप में:

public static extern bool StreamReceiveInitialise(string filepath); 
+0

धन्यवाद यह काम करता है। – integra753

1

इसे इस तरह कार्य करें:

[DllImport("Source.dll", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.ANSI)] 
static extern bool StreamReceiveInitialise([MarshalAs(UnmanagedType.LPStr)] string filepath); 

(UnmanagedType.LPStr के रूप में मार्शलिंग डिफ़ॉल्ट है, लेकिन मुझे स्पष्ट होना पसंद है)।

+0

वह [मार्शलए] विशेषता अनावश्यक है, यह इस मामले में स्ट्रिंग के लिए पहले ही डिफ़ॉल्ट मार्शलिंग है। –

+0

लेकिन 'DllImport' विशेषता पर 'CharSet' निर्दिष्ट करना एक बुरा विचार नहीं होगा। –

+0

मुझे उल्लेख करना चाहिए था कि मुझे सी कोड के भीतर फ़ाइलपैथ संशोधित करने की आवश्यकता नहीं है। क्या यह आपकी टिप्पणियां बदलता है? क्षमा करें मैं सी/सी # मिश्रण करने के लिए नया हूँ। – integra753

1

char * के स्थान पर एक स्ट्रिंगबिल्डर का उपयोग करें। देखें this

[DllImport("Source.dll")] 
public static extern bool StreamReceiveInitialise(StringBuilder filepath); 
संबंधित मुद्दे