Thread: string and dll

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    string and dll

    my dll function is ;
    Code:
    char* _stdcall pages(char* pageID){
    	char *p = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n<HTML><HEAD>\n<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">\n</HEAD>\n<BODY><p>";
    if (pageID == 'm') {
    	char* p2 = "madzombie deneme";
    } else {
    char* p2 = "deneme";
    }
    	char* p3 = "\n<p>\n</BODY></HTML>\n";
    	int l1 = StringLength(p);
    	int l2=StringLength(p2);
    	int l3=StringLength(p3);
    	int toplam = l1+l2+l3;
    	char* sonuc = new char[toplam];
    	strcpy(sonuc,p);
    	strcat(sonuc,p2);
    	strcat(sonuc,p3);
    	
    	return sonuc;
    }
    But the function give an error and my project time-out.
    char p1 and char p3 are static variable. char p2 is dynamic variable.
    what can i do ?

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    and StringLength function;
    Code:
    int StringLength(char inputString[])
    {
        int length = 0;
        for (int i = 0; inputString[i]!= '\0'; i++)
            length++;
        return length;
    }

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    i write on Dev-cpp and this codes runnig. But it isn't running on VS 2008 win32 dll project.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    using namespace std;
    char* _stdcall denem();
    int StringLength(char inputString[]);
    int main(int argc, char *argv[])
    {
    
       cout << denem();
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    char* _stdcall  denem(){
       char *p = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n<HTML><HEAD>\n<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">\n</HEAD>\n<BODY><p>";
    	char* p2 = "madzombie deneme";
    	char* p3 = "\n<p>\n</BODY></HTML>\n";
    	int l1 = StringLength(p);
    	int l2=StringLength(p2);
    	int l3=StringLength(p3);
    	int toplam = l1+l2+l3;
    	char* sonuc = new char[toplam];
    	strcpy(sonuc,p);
    	strcat(sonuc,p2);
    	strcat(sonuc,p3);
    
        return sonuc;
    }
    int StringLength(char inputString[])
    {
        int length = 0;
        for (int i = 0; inputString[i]!= '\0'; i++)
            length++;
        return length;
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What do you mean by "isn't running"? Does it not build? One thing to note is that a DLL doesn't usually have a standard main entry point. And _stdcall is __stdcall (you need another _).

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    5

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need to add 1 to toplam to make room for the null-terminator.

    p, p2, and p3 should be const.

    If you're willing to use strcpy and strcat, then use strlen as well.

    Your DLL needs to have a FreeString() function so that the memory returned denem() can be deleted.

    gg

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The DLL built fine for me once I added the project to a solution in VS 2008. But you've still not told us what exactly the problem is, so that's the best I can tell you. And don't forget codeplug's feedback.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    My Win32 dll function ;
    Code:
    char* _stdcall pages(){
    	const char* p ="deneme 1";
    	const char* p2 = "deneme 2";
    	int p_len = strlen(p);
    	int p2_len = strlen(p2);
    	int toplam = p_len + p2_len + 1;
    	char* yeni = new char[toplam];
    	strcpy(yeni,p);
    	strcat(yeni,p2);
    	return yeni;
    }
    My C# Project;

    Code:
         [DllImport("dlldeneme.dll")]
            public static extern string pages();

    Code:
         private void button1_Click(object sender, EventArgs e)
            {
                string cevap = pages();
                listBox1.Items.Add(cevap);
            }
    Error Message;
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I'm no C# guru - but you probably need a marshaling attribute like "[MarshalAs(UnmanagedType.LPTStr)] String".

    http://msdn.microsoft.com/en-us/library/s9ts558h.aspx

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. DLL Global Object Constructor String Error
    By n00b3 in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2008, 07:42 PM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Need Help passing a string from VB into a C++ DLL??
    By dan Burke in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2004, 09:15 AM
  5. returning pointer to string between exe and dll
    By evilpope in forum C Programming
    Replies: 2
    Last Post: 05-15-2003, 12:16 PM