problem in creating a dll
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: problem in creating a dll

  1. #1
    Join Date
    Mar 2009
    Posts
    20

    Post problem in creating a dll

    hey guys i m trying to create a dll of a function so that i can use it in my c# program ..but on running the program an error occurs of

    Debug error
    runtime check failiure no 2 : stack around variable pth was corrupted


    any suggestions what am i doing wrong here is the code
    Code:
    // code1.cpp : Defines the entry point for the DLL application.
    //
    #include "stdafx.h"
    //#include <iostream.h>
    #include <winnt.h>
    #include <Winbase.h>
    #include <conio.h>
    #include<string.h>
    #include<malloc.h>
    
    
    #ifdef _MANAGED
    #pragma managed(push, off)
    #endif
    
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
    					 )
    {
        return TRUE;
    }
    
    
    
    
    extern "C"
    {
    	__declspec(dllexport) bool WriteSector(char* path,int sector,int index,int code)
    {
    	char* buff = (char*)malloc (512);
    
    HANDLE h;
    DWORD br;
    char pth[10] ="\\\\.\\";
    strcat(pth,path);
    h=CreateFileA(pth,GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); 
    SetFilePointer(h,(sector*512),NULL,FILE_BEGIN);
    ReadFile(h,buff,512,&br,NULL);
    
    if(code==1)
    buff[index]=64;
    if(code==2)
    buff[index]=32;
    if(code==3)
    buff[index]=16;
    DWORD bytesN=0;
    SetFilePointer(h,512*sector,NULL,FILE_BEGIN);
    bool g=WriteFile(h,buff,512,&bytesN,NULL);
    CloseHandle(h);
     return g;
    }
    
    
    //int _tmain(int argc, TCHAR* argv[])
    
    }
    
    #ifdef _MANAGED
    #pragma managed(pop)
    #endif


    this is the signature i am using for c#

    Code:
    [DllImport("code1.dll")]
            public static extern bool WriteSector(String path, int sector, int index, int code);
    any suggestion what iam doin wrong plz

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: problem in creating a dll

    Code:
    char pth[10] ="\\\\.\\";
    strcat(pth,path);
    pth is only 10 long... how long is path ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center